错误:无法在“用户?”上访问属性“uid”因为它可能为空

问题描述 投票:0回答:2

嗨,flutter 和 firebase 新手,我想做的是简单地注册新用户,然后使用自定义 id 将其信息添加到 cloud firestore 并添加子集合名称“通知” 这里是代码示例..

final _auth = FirebaseAuth.instance;
  final _ref = FirebaseFirestore.instance;
  final userCollections = FirebaseFirestore.instance.collection("users");
  Future<void> SignupToColud(
      {@required String name = "",
      @required String email = "",
      @required String pass = ""}) async {
    try {
      await _auth
          .createUserWithEmailAndPassword(email: email, password: pass)
          .then((value) {
        String userId = value.user.uid;
        if (userId != null) {
          userCollections.doc(userId).set({'name': name, 'email': email});
          userCollections
              .doc(userId)
              .collection("notifications")
              .add({'txt': 'welcome you registered as new user succefully'});
        } else {
          throw Error();
        }
      });
      // first sign up to cloud if success

      //then add to users collections
    } catch (e) {
      print(e.toString());
    }
  }

i got this error !!

    lib/Sign_up.dart:24:36: Error: Property 'uid' cannot be accessed on 'User?' because it is potentially null.
 - 'User' is from 'package:firebase_auth/firebase_auth.dart' ('/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-1.2.0/lib/firebase_auth.dart').
Try accessing using ?. instead.
        String userId = value.user.uid;
                               ^^^

this is pubspec.yaml file information environment: sdk: ">=2.12.0 <3.0.0"

dependencies:

    lib/Sign_up.dart:24:36: Error: Property 'uid' cannot be accessed on 'User?' because it is potentially null.
 - 'User' is from 'package:firebase_auth/firebase_auth.dart' ('/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth-1.2.0/lib/firebase_auth.dart').
Try accessing using ?. instead.
        String userId = value.user.uid;
                               ^^^

我真的不知道这个问题以及如何解决它:(

firebase flutter firebase-authentication
2个回答
2
投票

错误来自这一行:

  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.0
  firebase_core: ^1.2.0
  cloud_functions: ^1.1.0
  cloud_firestore: ^2.2.0
  firebase_storage: ^8.1.0
  firebase_auth: ^1.2.0

正如错误所说,

String userId = value.user.uid;
是一个
value.user
,这意味着它可以是一个
User?
对象它可以是
User
。您的代码需要处理可能为空的事实,因为只有您可以决定在这种情况下做什么。

我想你想要什么:

null

因此,这会检查

  _auth
      .createUserWithEmailAndPassword(email: email, password: pass)
      .then((value) {
    if (value.user != null) {
      String userId = value.user!.uid;
      userCollections.doc(userId).set({'name': name, 'email': email});
是否为空(而不是检查
value.user
)。如果
userId
不为空,则保证
value.user
有一个值,因此我们可以在写入操作中使用它。


0
投票

867265505 请 meri ID wapas kar do maine koi galti nahin kari hai meri ID Bina Reason ke pahan kar di gai hai 请 meri ID ko wapas kar do maine koi galti nahin kari hai meri ID Bina Reason ban kar Di gai hai ek ID thi mere请通过 khelne wali vahi aapane ban kar Di 请 Bina Reason ke bahan Hui hai meri id 1.5 sal se meri id ban hai 请 867265505 请检查 karke dekho 的 ID meri koi galti nahin hai meri id wapas kar do aaj meri ID wapas kar dena 请ek aad ghante mein meri ID wapas kar do meri ID mein time chala do chahe 到 10 sal ka time chala do meri ID mein Dil Ko s867265505😂😔🙋🙋

abart Ho jaega Na meri ITI mein chahe time chala do 5 sal ka 10 sal ka jitna aapko achcha Lage to meri ID mein time chala do please meri ID to wapas kar do meri ID banaa dijiye please

© www.soinside.com 2019 - 2024. All rights reserved.