我正在尝试接收用于用户身份验证的 OTP。但是,我没有收到任何 OTP,我收到了这个错误。
这里是错误:
<块引用>E/flutter (30379): [错误:flutter/lib/ui/ui_dart_state.cc(199)]
未处理的异常:NoSuchMethodError:getter 'instance' 是
调用 null。 E/颤振 (30379): 接收器:空 E/颤振 (30379):
尝试调用:实例 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/flutter (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 Element.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 Element.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 Element.rebuild
(包: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 Element.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 Element.inflateWidget
(包:flutter/src/widgets/framework.dart:3611:14) E/flutter
(30379):#18 Element.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 Element.inflateWidget
(包:flutter/src/widgets/framework.dart:3611:14) E/flutter
(30379):#21 Element.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 Element.rebuild
(包: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 Element.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 Element.inflateWidget
(包:flutter/src/widgets/framework.dart:3611:14) E/flutter
(30379):#32 Element.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 Element.inflateWidget
(包:flutter/src/widgets/framework.dart:3611:14) E/flutter
(30379):#35 Element.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 Element.rebuild
(包: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 ComponentElement.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 Element.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();
}
}
谢谢!
答案 0 :(得分: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