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

问题描述

错误:

E/flutter (22800): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:NoSuchMethodError:getter 'phone' 被调用为 null。E/flutter (22800): 接收者: null E/flutter (22800): 尝试呼叫: phone 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.

这是我的代码:

    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();
  }
}

谢谢!

标签: firebaseflutter

解决方案


在您的 verifyPhone() 函数中,为什么要声明“小部件”变量?

尝试删除它

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));

}


推荐阅读