【问题标题】:The property 'uid' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.')无法无条件访问属性“uid”,因为接收者可以为“null”。尝试使访问有条件(使用'?.')
【发布时间】:2022-01-11 14:59:08
【问题描述】:

我有三个错误:

  1. 错误:没有为类型“用户”定义吸气剂“用户”。尝试导入定义 'user' 的库,将名称更正为现有 getter 的名称,或定义名为 'user' 的 getter 或字段
  2. 错误:没有为类型“用户”定义吸气剂“用户”。尝试导入定义 'user' 的库,将名称更正为现有 getter 的名称,或定义名为 'user' 的 getter 或字段
  3. 错误:无法无条件访问属性“uid”,因为接收者可以为“null”。尝试使访问有条件(使用'?.')或向目标添加空检查('!')

    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:firebase_auth/firebase_auth.dart';

    class _AuthService {
      final FirebaseAuth _auth = FirebaseAuth.instance;
      final FirebaseFirestore _firestore = FirebaseFirestore.instance;

      //giriş yap
      Future<User> signIn(String email, String password) async {
        var user = await _auth.signInWithEmailAndPassword(
            email: email, password: password);
        return User.user;
      }

      //çıkış yap
      signOut() async {
        return await _auth.signOut();
      }

      //kayıt ol
      Future<User> createPerson(String name, String lastname,
          String telephoneNumber, String email, String password) async {
        var user = await _auth.createUserWithEmailAndPassword(
            email: email, password: password);

        await _firestore.collection('Person').doc(user.user.uid).set({
          'name': name,
          'lastname': lastname,
          'telephone': telephoneNumber,
          'email': email,
        });

        return User.user;
      }

【问题讨论】:

  • 我的意思是错误不言自明。您在用户上调用 .user,这是一个不存在的属性。最后一个也可以,你在一个可为空的属性上调用 .uid,所以你可能想考虑使用 user?.uid。

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

这里的最后一行看起来不对:

Future<User> signIn(String email, String password) async {
  var user = await _auth.signInWithEmailAndPassword(
      email: email, password: password);
  return User.user;
}

首先,User.user 中的 User 是一个类。您更有可能想要引用您在上面一行中声明的user 变量,因此:return user.user

所以这有更好的机会:

Future<User> signIn(String email, String password) async {
  var user = await _auth.signInWithEmailAndPassword(
      email: email, password: password);
  return user.user;
}

因为我知道我们会对user.user 感到困惑,所以我更喜欢给变量一个不同的名称:

Future<User> signIn(String email, String password) async {
  var credentials = await _auth.signInWithEmailAndPassword(
      email: email, password: password);
  return credentials.user;
}

您在createPerson 中似乎也遇到了同样的问题,因此您也必须在那里进行更改。

【讨论】:

    猜你喜欢
    • 2021-07-13
    • 2021-09-21
    • 2022-08-15
    • 2021-08-17
    • 2022-08-15
    • 2021-08-05
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多