简体   繁体   English

Flutter 离线更新后快照丢失数据

[英]Flutter snapshot lost data after update in offline

I created a simple application that displays a list of movies from Firestore DB.我创建了一个简单的应用程序来显示来自 Firestore DB 的电影列表。 When you tap the movie, the number of likes increases by one.当您点击电影时,喜欢的次数会增加一个。 The problem occurs when the application runs on Android and is offline.当应用程序运行在 Android 并且处于离线状态时,就会出现该问题。 If I add a new record and then like it twice, the title of the movie will disappear from the snapshot.如果我添加一个新记录然后点赞两次,电影的标题将从快照中消失。

Future<void> _addNewFilm() async {
  final moviesRef = FirebaseFirestore.instance.collection('movies').withConverter<Movie>(
        fromFirestore: (snapshot, _) => Movie.fromJson(snapshot.data()!),
        toFirestore: (movie, _) => movie.toJson(),
      );
  _counter++;
  moviesRef.add(
    Movie(title: 'Star Wars: A New Hope (Episode $_counter)', likes: 0),
  );
}

List of movies电影列表

class MoviesWidget extends StatefulWidget {
  @override
  _MoviesWidgetState createState() => _MoviesWidgetState();
}

class _MoviesWidgetState extends State<MoviesWidget> {
  final Stream<QuerySnapshot> _usersStream = FirebaseFirestore.instance.collection('movies').snapshots();

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: _usersStream,
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return Text('Something went wrong ${snapshot.error.toString()}');
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return Text("Loading");
        }

        snapshot.data!.docs.forEach((element) {
          print("Element data: ${element.data()}");
        });

        return ListView(
          children: snapshot.data!.docs.map((DocumentSnapshot document) {
            Map<String, dynamic> data = document.data()! as Map<String, dynamic>;
            return ListTile(
              onTap: () {
                FirebaseFirestore.instance
                    .collection('movies')
                    .doc(document.id)
                    .update({'likes': data['likes'] + 1 }
                    );
              },
              title: Text(data['title'] ?? "not set"),
              subtitle: Text(data['likes']?.toString() ?? "not set"),
            );
          }).toList(),
        );
      },
    );
  }
}

How I test:我如何测试:

  1. Device is online.设备在线。 I create new film.我创作新电影。 In console I see Element data: {title: Star Wars: A New Hope (Episode 1), likes: 0} - OK在控制台中,我看到Element data: {title: Star Wars: A New Hope (Episode 1), likes: 0} - OK
  2. I take the phone offline我将手机离线
  3. I create new film.我创作新电影。 In console I see Element data: {title: Star Wars: A New Hope (Episode 2), likes: 0} Element data: {title: Star Wars: A New Hope (Episode 1), likes: 0} - OK在控制台中,我看到Element data: {title: Star Wars: A New Hope (Episode 2), likes: 0} Element data: {title: Star Wars: A New Hope (Episode 1), likes: 0} - OK
  4. I click to Episode 1. In console I see I/flutter ( 9675): Element data: {title: Star Wars: A New Hope (Episode 2), likes: 0} I/flutter ( 9675): Element data: {title: Star Wars: A New Hope (Episode 1), likes: 1} - OK我单击第 1 集。在控制台中,我看到I/flutter ( 9675): Element data: {title: Star Wars: A New Hope (Episode 2), likes: 0} I/flutter ( 9675): Element data: {title: Star Wars: A New Hope (Episode 1), likes: 1} - 好
  5. I click to Episode 2. In console I see I/flutter ( 9675): Element data: {title: Star Wars: A New Hope (Episode 2), likes: 1} I/flutter ( 9675): Element data: {title: Star Wars: A New Hope (Episode 1), likes: 1} - OK我单击第 2 集。在控制台中,我看到I/flutter ( 9675): Element data: {title: Star Wars: A New Hope (Episode 2), likes: 1} I/flutter ( 9675): Element data: {title: Star Wars: A New Hope (Episode 1), likes: 1} - 好
  6. I click to Episode 2 again.我再次点击第 2 集。 In console I see I/flutter ( 9675): Element data: {likes: 2} I/flutter ( 9675): Element data: {title: Star Wars: A New Hope (Episode 1), likes: 1} - ERROR title element lost.在控制台中,我看到I/flutter ( 9675): Element data: {likes: 2} I/flutter ( 9675): Element data: {title: Star Wars: A New Hope (Episode 1), likes: 1} -错误标题元素丢失。

Please help what I'm doing wrong.请帮助我做错了什么。

Versions版本

  • cloud_firestore: ^3.1.10 cloud_firestore:^3.1.10
  • firebase_core: ^1.13.1 firebase_core: ^1.13.1
  • flutterfire_ui: ^0.3.5+1 flutterfire_ui: ^0.3.5+1
  • Flutter 2.10.3 Flutter 2.10.3
  • Dart 2.16.1 Dart 2.16.1

Note: This doesn't work on Android. It works on iOS.注意:这不适用于 Android。它适用于 iOS。

Update: I added issue to cloud_firestore更新:我向cloud_firestore添加了问题

Update2: The issue also occurs in the native application. Update2:该问题也发生在本机应用程序中。 I added issue to cloud_firestore_native我向cloud_firestore_native添加了问题

Update3: This statement from the flutter team tends to lead to a problem in the main library. Update3:来自 flutter 团队的这个声明往往会导致主库出现问题。

Issue will fixed in new version of firestore native library (version 24.1.0).问题将在新版本的 firestore 本机库(版本 24.1.0)中修复。Watch this and this .看这个这个 I don't known when it will in flutter library. flutter库不知道什么时候能到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM