1

使用 FutureBuilder 使用返回 Firebase 身份验证用户凭据的未来“firebaseAuthSvc.signinwithphoneno(widget.pin,widget.verificationID)”构建自身时,我收到以下异常。

════════ 小部件库捕获的异常═════════════════════════════␕══以下_CastError 被抛出构建 FutureBuilder(脏,状态:_FutureBuilderState#a7b44):空值检查运算符用于空值

相关的导致错误的小部件是 FutureBuilder lib/screens/enterpinsubwidgetsecondscreen.dart:34 当抛出异常时,这是堆栈 #0 UserInfo.uid package:firebase_auth_platform_interface/src/user_info.dart:52 #1 UserInfo.toString package: firebase_auth_platform_interface/src/user_info.dart:57 #2 StringBuffer.write (dart:core-patch/string_buffer_patch.dart:64:22) #3 StringBuffer.writeAll (dart:core-patch/string_buffer_patch.dart:102:7) # 4 IterableBase.iterableToFullString (dart:collection/iterable.dart:267:14)

我使用未来构建器的有状态小部件如下。

class _EnterPinSubWidgetSecondScreenState
    extends State<EnterPinSubWidgetSecondScreen> {
  FirebaseAuthSvc firebaseAuthSvc = new FirebaseAuthSvc();
  Future<UserCredential> _usercredential ;
  @override
  @override
  void initState() {
    super.initState();
    print("Pin:${widget.pin} VerificationID: ${widget.verificationID}");
    _usercredential =
       firebaseAuthSvc.signinwithphoneno(widget.pin, widget.verificationID);
  }

  @override
  Widget build(BuildContext context) {
    print("Buildcontext of EnterPinSubWidgetSecondScreen");
    return FutureBuilder<UserCredential>(
      future: _usercredential,
      builder: (context, snapshot) {
        print(
            "in EnterPinSubWidgetSecondScreen builder ${snapshot.connectionState} data= ${snapshot.data}");
        if (snapshot.hasError) {
          print("Some error in snapshot ${snapshot.error}");
          return EnterPin();
        }

        if (snapshot.connectionState == ConnectionState.done) {
          
          return EnterPinSubWidgetThirdScreen();
        }

       
        return LoadingScr("Signining in...");
      },
    );
  }
}

我未来使用 firebase 电话身份验证 signinwithcredential 的定义为

Future<UserCredential> signinwithphoneno(
      String pin, String verificationID) async {
    String smsCode = pin;
    FirebaseAuth auth = FirebaseAuth.instance;

    // Create a PhoneAuthCredential with the code
    PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.credential(
        verificationId: verificationID, smsCode: smsCode);
    // Sign the user in (or link) with the credential
     return auth.signInWithCredential(phoneAuthCredential).then((user) {
        print("User not null ${user.user.phoneNumber}");

       return user;
     
    }).onError((error, stackTrace) {
      print("signingwithcredential returned error  $error");
      return null;
    });
  }
4

0 回答 0