2

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.

5
  • Are you using kotlin code in Flutter? Commented Oct 1, 2018 at 17:33
  • Nope, I just found a tutorial how to use firestore offline on kotlin. And i want to use it on flutter dart. Commented Oct 1, 2018 at 22:45
  • did you find any solution when the device comes online? Commented Dec 3, 2018 at 13:04
  • 1
    Hi @rickdroio nothing todo, all already handled by the sdk. Commented Dec 3, 2018 at 13:31
  • How did you solve this with flutter ? can you please write your code of how to make sure document is completed writing to cache ? Commented Feb 17, 2024 at 7:14

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.