我是學習顫振的初學者。所以,這段代碼給了我不可為空的錯誤。
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:sf_haven/pages/homePage.dart';
enum LoginVerification{
DEVICE_VERIFICATION_STATE,
OTP_VERIFICATION_STATE
}
class LoginPage extends StatefulWidget {
const LoginPage({ Key? key }) : super(key: key);
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
LoginVerification cState = LoginVerification.DEVICE_VERIFICATION_STATE;
final deviceController = TextEditingController();
final otpController = TextEditingController();
FirebaseAuth _auth = FirebaseAuth.instance;
String verificationId; //I'm getting that error here
bool showLoading = false;
void signInWithPhoneAuthCredential(PhoneAuthCredential phoneAuthCredential) async{
setState(() {
showLoading = true;
});
try {
final authCredential = await _auth.signInWithCredential(phoneAuthCredential);
setState(() {
showLoading = false;
});
if(authCredential?.user != null){
Navigator.push(context, MaterialPageRoute(builder: (context)=> HomePage()));
}
} on FirebaseAuthException catch (e) {
setState(() {
showLoading = false;
});
_scaffoldkey.cState.showSnackBar(SnackBar(content: Text(e.message))); //here at cState and e.message
}
}
getDeviceFormWidget(context){
return Column(
children:[
Spacer(),
TextField(
controller: deviceController,
decoration: InputDecoration(
hintText: "Phone Number",
),
),
SizedBox(
height: 16,
),
FlatButton(
onPressed: () async{
setState(() {
showLoading = true;
});
await _auth.verifyPhoneNumber(
phoneNumber: deviceController.text,
verificationCompleted: (phoneAuthCredential) async{
setState(() {
showLoading = false;
});
//signInWithPhoneAuthCredential(phoneAuthCredential;)
},
verificationFailed: (verificationFailed) async{
setState(() {
showLoading = false;
});
_scaffoldkey.cState.showSnackBar(SnackBar(content: Text(verificationFailed.message))); //here at cState and verificationFailed.message
},
codeSent: (verificationId, resendingToken) async{
setState((){
showLoading = false;
cState = LoginVerification.OTP_VERIFICATION_STATE;
this.verificationId = verificationId;
});
},
codeAutoRetrievalTimeout: (verificationId) async{
},
);
},
child: Text("Send"),
color: Colors.blue,
textColor: Colors.white,
),
Spacer(),
],
);
}
getOTPformWidget(context){
return Column(
children: [
Spacer(),
TextField(
controller: otpController,
decoration: InputDecoration(
hintText: "Enter OTP",
),
),
SizedBox(
height: 16,
),
FlatButton(
onPressed: () async{
PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.credential(verificationId: verificationId, smsCode: otpController.text);
signInWithPhoneAuthCredential(phoneAuthCredential);
},
child: Text("Verify"),
color: Colors.blue,
textColor: Colors.white,
),
Spacer(),
],
);
}
final GlobalKey<ScaffoldState> _scaffoldkey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldkey,
body: Container(
child: showLoading ? Center(child: CircularProgressIndicator(),) : cState == LoginVerification.DEVICE_VERIFICATION_STATE
? getDeviceFormWidget(context)
: getOTPformWidget(context),
padding: const EdgeInsets.all(13),
)
);
}
}
我收到不可為空的實體欄位 'verificationId' must be initialized error at verificationIdand cState。非常感謝....請忽略這部分,因為我正在嘗試通過字數來解決.....為什么我不能發布我的答案....
uj5u.com熱心網友回復:
只是改變:
String verificationId;
到:
String? verificationId;
“空安全”強制您說明變數何時為空,從而避免一些錯誤。所以,當一個變數可以為空并且沒有被初始化時,你應該使用 '?' 為了說明這一點,此外,當使用變數內部的資源時,可以為空,有必要確保值的存在,以便您不會訪問空值。例如,
ClientModel? date;
if (date!.name == "Username")....
使用“!”時 你確保在那個特定時刻這個變數不會為空。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/337359.html
標籤:扑 镖 firebase-身份验证
上一篇:如何在Flutter應用程式中重新安裝podfile.lock
下一篇:我怎樣才能用這些內容制作一個容器
