Reputation: 2668
I want to use offline firestore persistent, but I don't get the right code. On the kotlin tutorial said like this :
db.collection("members").document()
.addSnapshotListener(object : EventListener<DocumentSnapshot> {
override fun onEvent(snapshot: DocumentSnapshot?,
e: FirebaseFirestoreException?) {
if (e != null) {
Log.w(ContentValues.TAG, "Listen error", e)
err_msg.text = e.message
err_msg.visibility = View.VISIBLE;
return
}
snapshot?.reference?.set(data)
val intent = Intent()
setResult(Activity.RESULT_OK, intent)
[email protected]()
}
}
)
I try to use this code on flutter :
Firestore.instance.collection("hega").document().setData(data);
And also try this :
Firestore.instance.collection("hega").add(data);
Both works if online but not working when device is offline (using airplane mode)
UPDATE: So above code are actually works offline.
Firestore.instance.collection("hega").document().setData(data);
I just need to setup stream listener to the document snapshot, to see that the data is added to the local cache. But the other problem is when the device comes online, the offline data is not synced to server.
Upvotes: 2
Views: 2169
Reputation: 10453
You can enable offline data persistence like so as mentioned in the flutterfire docs.
For web
await FirebaseFirestore.instance.enablePersistence();
For other platforms
FirebaseFirestore.instance.settings = Settings(persistenceEnabled: false);
Upvotes: 3