如何解决此问题 [错误:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:NoSuchMethodError:getter 'phone' 被调用为 null。

Posted

技术标签:

【中文标题】如何解决此问题 [错误:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:NoSuchMethodError:getter \'phone\' 被调用为 null。错误?【英文标题】:How to fix this [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: The getter 'phone' was called on null. error?如何解决此问题 [错误:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:NoSuchMethodError:getter 'phone' 被调用为 null。错误? 【发布时间】:2021-09-24 13:00:55 【问题描述】:

错误:

E/flutter (22800): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:NoSuchMethodError:getter 'phone' 被调用为 null。 E/颤振(22800):接收器:空 E/flutter (22800):尝试呼叫:电话 E/flutter (22800): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) E/flutter (22800): #1 _OtpscreenState.verifyPhone (package:userauth/Otpscreen.dart:172:35) E/flutter (22800): #2 _OtpscreenState.initState (package:userauth/Otpscreen.dart:209:5) E/flutter (22800): #3 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4711:57) E/flutter (22800): #4 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5) E/flutter (22800): #5 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14) E/flutter (22800): #6 Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18) E/flutter (22800): #7 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6083:14) E/flutter (22800): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14) E/flutter (22800): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18) E/flutter (22800): #10 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16) E/flutter (22800): #11 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5) E/flutter (22800): #12 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4553:5) E/flutter (22800): #13 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5) E/flutter (22800): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14) E/flutter (22800): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18) E/flutter (22800): #16 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6083:14) E/flutter (22800): #17 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14) E/flutter (22800): #18 Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18) E/flutter (22800): #19 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6083:14) E/flutter (22800): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14) E/flutter (22800): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18) E/flutter (22800): #22 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16) E/flutter (22800): #23 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11) E/flutter (22800): #24 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5) E/flutter (22800): #25 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4553:5) E/flutter (22800): #26 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4737:11) E/flutter (22800): #27 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5) E/flutter (22800): #28 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14) E/flutter (22800): #29 Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18) E/flutter (22800): #30 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6083:14) E/flutter (22800): #31 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14) E/flutter (22800): #32 Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18) E/flutter (22800): #33 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6083:14) E/flutter (22800): #34 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14) E/flutter (22800): #35 Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18) E/flutter (22800): #36 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16) E/flutter (22800): #37 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11) E/flutter (22800): #38 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5) E/flutter (22800): #39 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4553:5) E/flutter (22800): #40 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4737:11) E/flutter (22800): #41 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4548:5) E/flutter (22800): #42 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14) E/flutter (22800): #43 Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18) E/flutter (22800): #44 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16) E/flutter (22800): #45 Element.rebuild (package:flutter/src/widgets/framework

这是我的代码:

    import 'package:flutter/material.dart';
import 'package:pinput/pin_put/pin_put.dart';
import 'package:pinput/pin_put/pin_put_state.dart';
import 'package:firebase_auth/firebase_auth.dart';

import 'package:firebase_core/firebase_core.dart';
import 'package:userauth/Profilescreen.dart';

class Otpscreen extends StatefulWidget 
  String phone;

  Otpscreen(this.phone);

  @override
  _OtpscreenState createState() => _OtpscreenState();


class _OtpscreenState extends State<Otpscreen> 


  final GlobalKey<ScaffoldState> _scaffoldkey = GlobalKey<ScaffoldState>();

  late String otp;
  late String _verificationCode;

  final TextEditingController _pinPutController = TextEditingController();
  final FocusNode _pinPutFocusNode = FocusNode();
  final BoxDecoration pinPutDecoration = BoxDecoration(
    color: Colors.blue,
    borderRadius: BorderRadius.circular(10.0),
    border: Border.all(
      color: const Color.fromRGBO(126, 203, 224, 1),
    ),
  );

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: Padding(
                padding: const EdgeInsets.only(top: 10, bottom: 10),
                child: Column(
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(right: 310),
                      child: RawMaterialButton(
                        onPressed: () 
                          Navigator.pop(context, "phone");
                        ,
                        child: Icon(Icons.arrow_back),
                      ),
                    ),
                    SizedBox(height: 40),
                    Text(
                      "Verify Phone",
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 20,
                          color: Colors.black),
                    ),
                    SizedBox(height: 10),
                    Text(
                      "code is sent to $widget.phone",
                      style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                          color: Colors.grey),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(30.0),
                      child: PinPut(
                        fieldsCount: 6,
                        textStyle: const TextStyle(
                            fontSize: 25.0, color: Colors.white),
                        eachFieldWidth: 40.0,
                        eachFieldHeight: 55.0,
                        focusNode: _pinPutFocusNode,
                        controller: _pinPutController,
                        submittedFieldDecoration: pinPutDecoration,
                        selectedFieldDecoration: pinPutDecoration,
                        followingFieldDecoration: pinPutDecoration,
                        pinAnimationType: PinAnimationType.fade,
                        onSubmit: (pin) async 
                          try 
                            await FirebaseAuth.instance
                                .signInWithCredential(
                                    PhoneAuthProvider.credential(
                                        verificationId: _verificationCode,
                                        smsCode: otp))
                                .then((value) async 
                              if (value.user != null) 
                                Navigator.pushAndRemoveUntil(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => Profilescreen()),
                                    (route) => false);
                              
                            );
                           catch (e) 
                            FocusScope.of(context).unfocus();
                            _scaffoldkey.currentState!.showSnackBar(
                                SnackBar(content: Text("invalid OTP")));
                          
                        ,
                      ),
                    ),
                    SizedBox(
                      height: 20,
                    ),
                    Center(
                      child: Padding(
                        padding: const EdgeInsets.only(
                            left: 20, right: 20, top: 0, bottom: 10),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Center(
                              child: Text(
                                "Didn't receive an code?",
                              ),
                            ),
                            GestureDetector(
                              onTap: () 
                                Navigator.pushNamed(context, "phone");
                              ,
                              child: Center(
                                child: Text(
                                  "Request Again",
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontWeight: FontWeight.bold,
                                    fontSize: 16,
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    SizedBox(height: 20),
                    RawMaterialButton(
                      onPressed: () 
                        Navigator.pushNamed(context, "profile");
                      ,
                      fillColor: Colors.blue,
                      child: Padding(
                        padding: const EdgeInsets.fromLTRB(50, 15, 50, 15),
                        child: Text(
                          "VERIFY AND CONTINUE",
                          style: TextStyle(
                              fontSize: 16, fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  

  verifyPhone() async 
    var widget;
    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: "+91$widget.phone",
        verificationCompleted: (PhoneAuthCredential credential) async 
          await FirebaseAuth.instance
              .signInWithCredential(credential)
              .then((value) async 
            if (value.user != null) 
              Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(builder: (context) => Profilescreen()),
                  (route) => false);
            
          );
        ,
        verificationFailed: (FirebaseAuthException e) 
          print(e.message);
        ,
         codeSent: (String verificationID, int? resendToken) 
          setState(() 
            _verificationCode = verificationID;
          );
        ,


        codeAutoRetrievalTimeout: (String verificationID) 
          setState(() 
            var verificationCode = verificationID;

          );
          ,
        timeout: Duration(seconds: 60));

  

  @override
  void initState() 
    // TODO: implement initState
    super.initState();
    verifyPhone();
  

谢谢!

【问题讨论】:

您使用的是空安全吗? 不,我没有升级我的版本。 【参考方案1】:

在你的 verifyPhone() 函数中,你为什么要声明一个“widget”变量??

尝试删除它

verifyPhone() async 
var widget;  <<--- remove this from your code
await FirebaseAuth.instance.verifyPhoneNumber(
    phoneNumber: "+91$widget.phone",
    verificationCompleted: (PhoneAuthCredential credential) async 
      await FirebaseAuth.instance
          .signInWithCredential(credential)
          .then((value) async 
        if (value.user != null) 
          Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(builder: (context) => Profilescreen()),
              (route) => false);
        
      );
    ,
    verificationFailed: (FirebaseAuthException e) 
      print(e.message);
    ,
     codeSent: (String verificationID, int? resendToken) 
      setState(() 
        _verificationCode = verificationID;
      );
    ,


    codeAutoRetrievalTimeout: (String verificationID) 
      setState(() 
        var verificationCode = verificationID;

      );
      ,
    timeout: Duration(seconds: 60));

【讨论】:

【参考方案2】:

您正在尝试在此行中获取 widget.phone

var widget;
    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: "+91$widget.phone", <-

但是,代码不是将 phone 作为参数传递给 Otpscreen,而是获取 phone 变量,在函数内部声明。由于变量为 null,因此返回 NoSuchMethodError。尝试删除var widget 声明。

【讨论】:

以上是关于如何解决此问题 [错误:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:NoSuchMethodError:getter 'phone' 被调用为 null。的主要内容,如果未能解决你的问题,请参考以下文章

如何解决此问题 错误:无法解析包?

创建项目时如何解决此错误?

为啥颤振向我显示此错误,我该如何解决?

如何解决此科尔多瓦构建错误?

如何解决此编译错误

如何解决此解码和语法错误