【问题标题】:TypeError: Cannot read property 'uid' of null flutterTypeError:无法读取空颤动的属性“uid”
【发布时间】:2021-06-24 17:16:12
【问题描述】:

我目前在登录时遇到此错误消息,我的注册功能工作正常。但似乎无法弄清楚为什么我的登录总是带来这个错误。如果我能指出我的错误来自哪里,我将不胜感激。

void loginUser() async {
    User user;
    await _auth
        .signInWithEmailAndPassword(email: email.text, password: password.text)
        .then((value) {
      setState(() {
        loading = false;
      });
      return value;
    }).catchError((error) {
      Navigator.pop(context);
      Get.defaultDialog(title: 'Error in Login');
    });
    if (user != null) {
      readData(user).then((value) => Get.toNamed(homeRoute));
    }
  }

  Future readData(User user) async {
    FirebaseFirestore.instance
        .collection('users')
        .doc(user.uid)
        .get()
        .then((result) async {
      await App.sharedPreferences.setString('uid', result.get('uid'));
      await App.sharedPreferences.setString('email', result.get('email'));
      await App.sharedPreferences.setString('fullname', result.get('fullname'));
      await App.sharedPreferences.setString('bvn', result.get('bvn'));
    });
  }

这是我的注册功能

 void _registerUser() async {
    User user;
    await _auth
        .createUserWithEmailAndPassword(
            email: email.text, password: password.text)
        .then((value) {
      user = value.user;
    }).catchError((error) {
      Navigator.pop(context);
      Get.defaultDialog(title: 'Error in registration');
    });
    if (user != null) {
      saveToFirestore(user).then((value) => Get.toNamed(homeRoute));
    }
  }

  Future saveToFirestore(User user) async {
    FirebaseFirestore.instance.collection('users').doc(user.uid).set({
      'uid': user.uid,
      'fullname': fullname.text.trim(),
      'email': user.email,
      'bvn': bvn.text
    });

    await App.sharedPreferences.setString('uid', user.uid);
    await App.sharedPreferences.setString('email', user.email);
    await App.sharedPreferences.setString('fullname', fullname.text);
    await App.sharedPreferences.setString('bvn', bvn.text);
  }

【问题讨论】:

    标签: flutter flutter-web


    【解决方案1】:

    改变

      User user;
        await _auth
    

    User user = await _auth....

    在您的 FireStore 查询中,使用

     Future readData(User user) async {
       FirebaseFirestore.instance
          .collection('users') 
          .doc(user.user.uid) // instead of user.uid
          .get()
    

    任何带有user.uid 的地方都将其更改为user.user.uid

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 2018-10-09
      • 2021-11-24
      • 2021-06-19
      • 1970-01-01
      • 2022-08-13
      • 2019-02-05
      • 2022-01-12
      • 2018-11-06
      相关资源
      最近更新 更多