'Flutter snapshot lost data after update in offline
I created a simple application that displays a list of movies from 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. 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:
- Device is online. I create new film. In console I see
Element data: {title: Star Wars: A New Hope (Episode 1), likes: 0}
- OK - I take the phone offline
- 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 - 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 - 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 - I click to Episode 2 again. 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.
Please help what I'm doing wrong.
Versions
- cloud_firestore: ^3.1.10
- firebase_core: ^1.13.1
- flutterfire_ui: ^0.3.5+1
- Flutter 2.10.3
- Dart 2.16.1
Note: This doesn't work on Android. It works on iOS.
Update: I added issue to cloud_firestore
Update2: The issue also occurs in the native application. I added issue to cloud_firestore_native
Update3: This statement from the flutter team tends to lead to a problem in the main library.
Solution 1:[1]
Issue will fixed in new version of firestore native library (version 24.1.0) . Watch this and this. I don't known when it will in flutter library.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |