【问题标题】:Null uid on firestore attempt to save data with flutter and firebaseFirestore上的空uid尝试使用flutter和firebase保存数据
【发布时间】:2020-05-09 16:31:11
【问题描述】:

我正在尝试在用户注册时保存一些数据,但我可能没有调用 FirebaseUser 权限。有什么建议?

代码:

String errorMessage;
  String name;
  String password;
  String email;
  String role;
  bool obscure = true;
  FirebaseUser instructor;

  final FirebaseAuth _auth = FirebaseAuth.instance;

  final db = Firestore.instance;

  void saveData() async {
    await db.collection("instructori")
      .document(instructor.uid)
      .setData(
        {
          'Nume': name,
          'Rol': role,
          'Email': email,
          'Parola': password,
          'UID': instructor.uid
        }
      );
  }


  Future<void> registerUser() async {
    instructor = await _auth
        .createUserWithEmailAndPassword(
      email: email,
      password: password,
    ).then((onValue) {
      saveData();
      Navigator.of(context).pushNamed(ClientiPage.id);
    }).catchError((error) {
      errorMessage = error;
    });



    Navigator.of(context).pushNamed(ClientiPage.id);
  }

用户成功通过身份验证,但我没有看到数据被保存到 Firestore 树中。我在控制台中得到了一切工作,所以我认为它是代码。

错误:

E/flutter ( 5527): Tried calling: uid
E/flutter ( 5527): #0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:53:5)
E/flutter ( 5527): #1      _RegisterPageState.saveData 
package:euroswimming_instructori/register_page.dart:29
E/flutter ( 5527): #2      _RegisterPageState.registerUser.<anonymous closure> 
package:euroswimming_instructori/register_page.dart:48
E/flutter ( 5527): #3      _rootRunUnary  (dart:async/zone.dart:1134:38)

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore firebase-authentication


    【解决方案1】:

    你应该这样做:

    instructor = (await FirebaseAuth.instance.currentUser()).uid;
    

    能够获取用户的uid

    currentUser 返回一个Future&lt;FirebaseUser&gt; 类型的值,因此您使用await 能够检索currentUser

    https://dart.dev/codelabs/async-await#working-with-futures-async-and-await

    https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_auth/firebase_auth/lib/src/firebase_auth.dart#L314

    注意

    uid 的类型为 String,因此如果您希望 instructor 的类型为 FirebaseUser,请执行以下操作:

    instructor = await FirebaseAuth.instance.currentUser();
    String userId = instructor.uid;
    

    【讨论】:

      【解决方案2】:

      .then() 返回一个不同的Future&lt;void&gt;,所以instructor 被设置为null。您可以像这样在.then() 回调中设置instructor

      void registerUser() {
        _auth.createUserWithEmailAndPassword(
          email: email,
          password: password,
        ).then((result) {
          instructor = result.user;
          saveData();
          Navigator.of(context).pushNamed(ClientiPage.id);
        }).catchError((error) {
          errorMessage = error;
        });
      
        Navigator.of(context).pushNamed(ClientiPage.id);
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-24
        • 2021-08-27
        • 1970-01-01
        • 2022-06-15
        • 2021-03-16
        • 2018-11-15
        • 2019-03-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多