I'm a beginner in learning flutter. so, this code gives me non nullable error.
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),
)
);
}
}
I'm getting non nullable instance field 'verificationId' must be initialized error at verificationId
and cState
. Much thanks in advance.... Please Ignore this part as I'm trying to get past through the amount of words..... Why can't I post my answer though....
CodePudding user response:
Just change:
String verificationId;
to:
String? verificationId;
The "Null Safety" forces you to say when a variable will be null or not, thus avoiding some errors. So, when a variable can be null and is not being initialized, you should use '?' to show this, moreover when using resources that are inside the variable that can be null, it is necessary to ensure the existence of a value so that you do not access a null value. For example,
ClientModel? date;
if (date!.name == "Username")....
when using "!" you ensure that at that particular moment this variable will not be null.