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

Posted

技术标签:

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

我正在尝试接收用于用户身份验证的 OTP。但是,我没有收到任何 OTP,我收到了这个错误。

这是错误:

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

这是我的代码:

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: pin))
                                .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 firebaseAuth;
    var widget;
    await firebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: "+91$widget.phone",
        verificationCompleted: (PhoneAuthCredential credential) async 
          await firebaseAuth.instance
              .signInCredential(credential)
              .then((value) async 
            if (value.user > 0) 
              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】:

这是错误:

    var firebaseAuth;
    var widget;
    await firebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: "+91$widget.phone",
        verificationCompleted: (PhoneAuthCredential credential) async 
          await firebaseAuth.instance

firebaseAuth 没有引用任何对象,因此您得到:

getter 'instance' 在 null 上被调用。

因此,删除不需要的变量,并且由于属性instance 返回static FirebaseAuth,那么您必须执行以下操作:

    var widget;
    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: "+91$widget.phone",
        verificationCompleted: (PhoneAuthCredential credential) async 
          await FirebaseAuth.instance

【讨论】:

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

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

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

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

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

如何解决此编译错误

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