i am trying to get the text kept in the validator property in the TextFormField to show in the app but for some reason, it keeps keeps giving me the LateInitializationError: Field 'email' has not been initialized. even though i have initialized my email and password in the onChanged property. Here is the code
class SellersLoginScreen extends StatefulWidget {
const SellersLoginScreen({Key? key}) : super(key: key);
static const String routName = 'SellersLoginScreen';
@override
State<SellersLoginScreen> createState() => _SellersLoginScreenState();
}
class _SellersLoginScreenState extends State<SellersLoginScreen> {
final FirebaseAuth _auth = FirebaseAuth.instance;
bool passwordVisible = true;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
late String email;
late String password;
void loginSeller() async{
try{
if(_formKey.currentState!.validate()){
await _auth.signInWithEmailAndPassword(
email: email, password: password
);
}else{
return snackBar('Please fields must not be empty', context);
}
}catch(e){
return print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(10.0),
child:Center(
child: SingleChildScrollView(
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Sign in To Seller Account',
style: TextStyle(
fontFamily: 'Typett',
fontStyle: FontStyle.italic,
fontSize: 20,
fontWeight: FontWeight.bold
),
),
IconButton(
onPressed: (){},
icon: Icon(
Icons.person,
size: 35,
color: Colors.cyan,
)
)
],
),
SizedBox(
height: 15,
),
TextFormField(
onChanged: (String value){
email = value;
},
validator: (value){
if(value!.isEmpty){
'please email address must not be empty';
}else{
return null;
}
},
decoration: InputDecoration(
labelText: 'Email',
hintText: 'Enter your Email',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25),
),
),
),
SizedBox(
height: 10,
),
TextFormField(
onChanged: (String value){
password = value;
},
validator: (value){
if(value!.isEmpty){
'please password field must not be empty';
}else{
return null;
}
},
obscureText: passwordVisible,
decoration: InputDecoration(
suffixIcon: IconButton(
onPressed: (){
setState(() {
passwordVisible = !passwordVisible;
});
},
icon: passwordVisible? Icon(
Icons.visibility,
):Icon(Icons.visibility_off)
),
labelText: 'Password',
hintText: 'Enter your Password',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25),
),
),
),
SizedBox(
height: 15,
),
GestureDetector(
onTap: (){
loginSeller();
},
child: Container(
width: MediaQuery.of(context).size.width - 40,
height: 50,
decoration: BoxDecoration(
color: Colors.cyan,
borderRadius: BorderRadius.circular(15)
),
child: Center(
child: Text('Login',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold
),
),
),
),
),
perhaps there is something i am missing. i have tried replacing the late with an empty string but it still didnt work. help is very much needed
CodePudding user response:
Replace:
late String email;
to:
String? email;
CodePudding user response:
try this:
class SellersLoginScreen extends StatefulWidget {
const SellersLoginScreen({Key? key}) : super(key: key);
static const String routName = 'SellersLoginScreen';
@override
State<SellersLoginScreen> createState() => _SellersLoginScreenState();
}
class _SellersLoginScreenState extends State<SellersLoginScreen> {
final FirebaseAuth _auth = FirebaseAuth.instance;
bool passwordVisible = true;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
late String email;
late String password;
void loginSeller() async{
try{
if(_formKey.currentState!.validate()){
await _auth.signInWithEmailAndPassword(
email: email, password: password
);
}else{
return snackBar('Please fields must not be empty', context);
}
}catch(e){
return print(e);
}
}
@override
void initState() {
email = "";
password="";
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(10.0),
child:Center(
child: SingleChildScrollView(
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Sign in To Seller Account',
style: TextStyle(
fontFamily: 'Typett',
fontStyle: FontStyle.italic,
fontSize: 20,
fontWeight: FontWeight.bold
),
),
IconButton(
onPressed: (){},
icon: Icon(
Icons.person,
size: 35,
color: Colors.cyan,
)
)
],
),
SizedBox(
height: 15,
),
TextFormField(
onChanged: (String value){
email = value;
},
validator: (value){
if(value!.isEmpty){
'please email address must not be empty';
}else{
return null;
}
},
decoration: InputDecoration(
labelText: 'Email',
hintText: 'Enter your Email',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25),
),
),
),
SizedBox(
height: 10,
),
TextFormField(
onChanged: (String value){
password = value;
},
validator: (value){
if(value!.isEmpty){
'please password field must not be empty';
}else{
return null;
}
},
obscureText: passwordVisible,
decoration: InputDecoration(
suffixIcon: IconButton(
onPressed: (){
setState(() {
passwordVisible = !passwordVisible;
});
},
icon: passwordVisible? Icon(
Icons.visibility,
):Icon(Icons.visibility_off)
),
labelText: 'Password',
hintText: 'Enter your Password',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25),
),
),
),
SizedBox(
height: 15,
),
GestureDetector(
onTap: (){
loginSeller();
},
child: Container(
width: MediaQuery.of(context).size.width - 40,
height: 50,
decoration: BoxDecoration(
color: Colors.cyan,
borderRadius: BorderRadius.circular(15)
),
child: Center(
child: Text('Login',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold
),
),
),
),
),
CodePudding user response:
use
String? email;
and the initState
to evaluate and set the value to the email variable.
CodePudding user response:
just write
String email = "";
String password = "";
instead of late keyword as it cannot be null and need to be initialized as well.