firebase - 如何解决此问题 [错误: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();
}
}
谢谢!
解决方案
在您的 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));
}
推荐阅读
- javascript - WebSocket 连接无效帧头
- macos - 如何在本地笔记本电脑上拥有源代码和 IDE,并在 docker 容器内的另一台机器上运行它?
- python - 无法捕获错误,而是打印到控制台
- php - PHP 缓冲区读取
- visual-foxpro - Foxpro Visual 9 中的字符串拆分
- r - 根据字符串模式匹配拆分数据框列
- php - shopware 5:无法加载模板片段
- python - 如何检测我的角色是否与敌人相撞并在命中 3 次后关闭游戏
- angular - 如何将 [checked] 与 precheck 和 ngModel 结合起来同时与函数绑定?
- python - 在 GraphQL 中过滤嵌入式文档列表