Flatter Firebase多个setData线程仅响应最后一个线程

Flatter Firebase多个setData线程仅响应最后一个线程,firebase,flutter,dart,google-cloud-firestore,firebase-authentication,Firebase,Flutter,Dart,Google Cloud Firestore,Firebase Authentication,使用多个setData线程写入Firebase数据库时,只解析最后一个线程。例如: Future registerWithEmailAndPassword(String email, String password) async { try { AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password); FirebaseUser

使用多个setData线程写入Firebase数据库时,只解析最后一个线程。例如:

Future registerWithEmailAndPassword(String email, String password) async {
    try {
      AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
      FirebaseUser user = result.user;
      await Firestore.instance.collection('stores').document(user.uid).setData({'String1': 'My First String', 'String2': 'My Second String', 'String3': 'My Third String'});
      await Firestore.instance.collection('stores').document(user.uid).setData({'String4': 'My Fourth String', 'String5': 'My Fifth String', 'String6': 'My Sixth String'});
      return userFromFirebaseUser(user);

    } catch(e) {
      print('REGISTER EnP ERROR: ' + e.toString());
      return null;
    }
  }
创建新用户时,Firebase数据库中显示以下结果:

String4: "My Fourth String"
String5: "My Fifth String"
String6: "My Sixth String"
需要能够像这样写入数据库,每次写入都应该在继续之前等待完成,但这似乎并没有发生

为BarBaECU编辑:

实际代码是,我通过类传递数据,而不是直接使用setData编写。因此,不是:

await Firestore.instance.collection('stores').document(user.uid).setData({'String1': 'My First String', 'String2': 'My Second String', 'String3': 'My Third String'});
await Firestore.instance.collection('stores').document(user.uid).setData({'String4': 'My Fourth String', 'String5': 'My Fifth String', 'String6': 'My Sixth String'});
它实际上是这样写的:

await DatabaseService1(uid: user.uid).updateUserData(variable1, variable2, variable3);
await DatabaseService2(uid: user.uid).updateUserData(variable4, variable5, variable6);
因此,对于批处理类型的解决方案,我不太确定如何编写语法,以便将这些类调用到批处理中,然后运行它。

请尝试以下操作:

 await Firestore.instance.collection('stores').document(user.uid).setData({'String1': 'My First String', 'String2': 'My Second String', 'String3': 'My Third String'});
 await Firestore.instance.collection('stores').document(user.uid).updateData({'String4': 'My Fourth String', 'String5': 'My Fifth String', 'String6': 'My Sixth String'});
在第一次写入时使用setData添加数据,在第二次写入时使用updateData添加新字段,当前第二个setData将覆盖第一个setData中的字段,这就是为什么在数据库中看不到String1、String2、String3。

请尝试以下操作:

 await Firestore.instance.collection('stores').document(user.uid).setData({'String1': 'My First String', 'String2': 'My Second String', 'String3': 'My Third String'});
 await Firestore.instance.collection('stores').document(user.uid).updateData({'String4': 'My Fourth String', 'String5': 'My Fifth String', 'String6': 'My Sixth String'});


在第一次写入时使用setData添加数据,在第二次写入时使用updateData添加新字段,目前第二个setData正在覆盖第一个setData中的字段,这就是为什么您在数据库中看不到String1、String2、String3。

请更新您的问题以显示updateUserData的功能。您好,Frank,我已按要求添加了代码。除非我运行另一个线程,否则在将数据填充到数据库中时,这一切都非常好。OHDatabaseService也完全一样。谢谢你的更新。我不知道这里出了什么问题。如果您简化了代码,不再使用DatabaseService类,而是使用Email和Password直接从注册表中调用setData,您仍然可以重现问题吗?非常感谢您的跟进,Frank。我听从了你的建议,直接使用了setData。在添加的两行中,它只再次处理第二行。完全忽略第一行。我将把测试过的代码添加到问题中。我不确定问题出在哪里,但是如果您也需要读取数据,为什么不使用批写入或事务呢?这样,您可以原子地更新数据,并确保两个操作都发生。请更新您的问题,以显示updateUserData的作用。嗨,Frank,我已按要求添加了代码。除非我运行另一个线程,否则在将数据填充到数据库中时,这一切都非常好。OHDatabaseService也完全一样。谢谢你的更新。我不知道这里出了什么问题。如果您简化了代码,不再使用DatabaseService类,而是使用Email和Password直接从注册表中调用setData,您仍然可以重现问题吗?非常感谢您的跟进,Frank。我听从了你的建议,直接使用了setData。在添加的两行中,它只再次处理第二行。完全忽略第一行。我将把测试过的代码添加到问题中。我不确定问题出在哪里,但是如果您也需要读取数据,为什么不使用批写入或事务呢?这样,您就可以原子地更新数据,并确保两个操作都发生了。@barbaecu是的,这是真的耶,这似乎就是问题所在!非常感谢彼得。我已将第二个类更改为使用updateData,而不是类内的setData。我现在正在测试是否可以稍后在应用程序中使用该类并使其处于这种状态。@Bisclavret没问题是的,首先您需要使用setData,然后您必须使用updateData或带有merge true的setData来添加新字段,而不覆盖Great catch Peter!好的,昨晚我有机会测试了它,我可以使用DatabaseService2类,只要我想调用它,就可以使用updateData。所以我会接受这个答案,因为它不会破坏任何东西;非常感谢你@是的,这是真的是的,这似乎是问题所在!非常感谢彼得。我已将第二个类更改为使用updateData,而不是类内的setData。我现在正在测试是否可以稍后在应用程序中使用该类并使其处于这种状态。@Bisclavret没问题是的,首先您需要使用setData,然后您必须使用updateData或带有merge true的setData来添加新字段,而不覆盖Great catch Peter!好的,昨晚我有机会测试了它,我可以使用DatabaseService2类,只要我想调用它,就可以使用updateData。所以我会接受这个答案,因为它不会破坏任何东西;非常感谢你!