【问题标题】:how to manage offline firebase database in flutter?如何在flutter中管理离线firebase数据库?
【发布时间】:2020-04-21 09:22:55
【问题描述】:

我已使用

管理离线 Firebase 数据
 Firestore.instance.settings(persistenceEnabled: true);

我从What is the best way to store data from Firebase to SQLite or offline in flutter?得到了这个解决方案

但现在它工作正常,但在 firebase 操作中它不会进入下一步。 我的代码是:

 showLoader();
   print("2222222222222222");

    DocumentReference ref = await _databaseReference
        .collection(FirebaseCollection.transcation)
        .add(param).catchError((onError){
          hideLoader();
          print("5555555  $onError");
    }).then((onValue){
      print("44444444444 $onValue");
    });

    print("33333333333");

    var token = '';
    var receiverId = '';
    .collection(FirebaseCollection.user)
      .document(userId)
      .get()
      .then((document) async {
    UserModel userModel = UserModel.fromsnapshot(document);
     print("data");
   }

这里我有调试但只打印 2222222222 没有别的。 当我的设备离线时,我无法隐藏我的加载程序或执行的其他任务。

【问题讨论】:

  • 添加一个 try-catch 块,如果有,请分享异常详细信息。

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

当您在 Firestore 中使用离线持久性时,请勿使用 Transactions 或 await 进行响应。

【讨论】:

    【解决方案2】:

    对我来说,当我实现 bloc 模式时,在存储库的构造函数中,我确实检查了连接状态,并根据我是否在线/离线。

    其余代码保持不变,所以我仍然使用 await 等。

    bool online = ConnectionStatusSingleton.getInstance().currentStatus;
        if(online){
          FirebaseDatabase.instance.goOnline();
          _databasePropertiesReference.keepSynced(true);
        }else{
          FirebaseDatabase.instance.goOffline();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 2017-05-24
      • 2020-01-12
      • 1970-01-01
      相关资源
      最近更新 更多