【问题标题】:How to add Data to firestore when internet connection is not working?互联网连接不工作时如何将数据添加到 Firestore?
【发布时间】:2021-08-17 20:40:09
【问题描述】:

我是 Flutter 的新手,我正在尝试在 Internet 连接不工作时将数据添加到 Firestore, 例如 - 我在添加数据之前检查了互联网连接,但是当我检查连接时我的互联网连接可以工作,而当我添加数据时,由于数据包耗尽或连接不稳定,我的互联网连接可能无法工作。在这种情况下,它不会返回错误。在颤振中,只有在数据添加到服务器后才会触发 Then(),因此,我该如何检查pendingWrites?我应该等到数据添加到缓存来检查pendingWrites。当 Internet 连接不工作时,添加数据的最佳方法是什么?

这是我的代码,

 SavePersonalData(this.firstName, this.lastName, this.birthDay, this.gender, 
this.userID, this.context);

 Future<void> saveUserData(){
return users.collection('users').doc(userID)
    .set({
  'first_name': firstName,
  'last_name': lastName,
  'birth_day': birthDay,
  'user_id': userID,
  "gender": gender==0?"Male":"Female",
  'account_created_on': FieldValue.serverTimestamp(),
})
    .then((value) => `//This is does not trigger until internet connection works`





 Navigator.push(context, MaterialPageRoute(builder: (context)=> SelectImage()))
print("data added to the databse")).timeout(Duration(seconds:5),onTimeout: ()=>{
  print("TimeOut")
})
    .catchError(
        (error) {
      print("Failed to add user: $error");
      
    } );

【问题讨论】:

    标签: firebase flutter google-cloud-firestore flutter-layout


    【解决方案1】:

    根据FlutterFire Documentation,默认启用离线持久化。

    Firebase Docs 表示如果您启用了离线持久性,它将在互联网连接可用时同步所有更改。虽然如果您的应用需要实时交互,那么您可能应该禁用它。

    值得注意的是,您不能在离线模式下使用事务。更多详情请见here。也正如这里提到的,你不需要在离线时使用 await。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      相关资源
      最近更新 更多