I cannot find the solution to this problem. I am creating a sign up screen connected to firebase but it is giving me this error, "The non-nullable local variable 'result' must be assigned before it can be used. Try giving it an initializer expression, or ensure that it's assigned on every execution path."
The error is here. On the first and third line under result.
FirebaseFirestore.instance.collection("User").doc(result.user!.uid).set({
"UserName": userName.text,
"UserId": result.user!.uid,
"UserEmail": email.text,
"UserAddress": address.text,
"UserGender": isMale == true ? "Male" : "Female",
"UserNumber": phoneNumber.text,
});
This is the whole code
`import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:e_commerce_application/screens/homepage.dart';
import 'package:e_commerce_application/screens/login.dart';
import 'package:e_commerce_application/widgets/changescreen.dart';
import 'package:e_commerce_application/widgets/mybutton.dart';
import 'package:e_commerce_application/widgets/mytextformField.dart';
import 'package:e_commerce_application/widgets/passwordtextformfield.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class SignUp extends StatefulWidget {
@override
_SignUpState createState() => _SignUpState();
}
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
String p =
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
RegExp regExp = new RegExp(p);
bool obserText = true;
final TextEditingController email = TextEditingController();
final TextEditingController userName = TextEditingController();
final TextEditingController phoneNumber = TextEditingController();
final TextEditingController password = TextEditingController();
final TextEditingController address = TextEditingController();
bool isMale = true;
bool isLoading = false;
class _SignUpState extends State<SignUp> {
void submit() async {
UserCredential result;
try {
setState(() {
isLoading = true;
});
result = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email.text, password: password.text);
print(result);
} on PlatformException catch (error) {
var message = "Please Check Your Internet Connection ";
if (error.message != null) {
message = error.message!;
}
_scaffoldKey.currentState!.showSnackBar(SnackBar(
content: Text(message.toString()),
duration: Duration(milliseconds: 600),
backgroundColor: Theme.of(context).primaryColor,
));
setState(() {
isLoading = false;
});
} catch (error) {
setState(() {
isLoading = false;
});
_scaffoldKey.currentState!.showSnackBar(SnackBar(
content: Text(error.toString()),
duration: Duration(milliseconds: 600),
backgroundColor: Theme.of(context).primaryColor,
));
print(error);
}
FirebaseFirestore.instance.collection("User").doc(result.user!.uid).set({
"UserName": userName.text,
"UserId": result.user!.uid,
"UserEmail": email.text,
"UserAddress": address.text,
"UserGender": isMale == true ? "Male" : "Female",
"UserNumber": phoneNumber.text,
});
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (ctx) => HomePage()));
setState(() {
isLoading = false;
});
}
void vaildation() async {
if (userName.text.isEmpty &&
email.text.isEmpty &&
password.text.isEmpty &&
phoneNumber.text.isEmpty &&
address.text.isEmpty) {
_scaffoldKey.currentState!.showSnackBar(
SnackBar(
content: Text("All Flied Are Empty"),
),
);
} else if (userName.text.length < 6) {
_scaffoldKey.currentState!.showSnackBar(
SnackBar(
content: Text("Name Must Be 6 "),
),
);
} else if (email.text.isEmpty) {
_scaffoldKey.currentState!.showSnackBar(
SnackBar(
content: Text("Email Is Empty"),
),
);
} else if (!regExp.hasMatch(email.text)) {
_scaffoldKey.currentState!.showSnackBar(
SnackBar(
content: Text("Please Try Vaild Email"),
),
);
} else if (password.text.isEmpty) {
_scaffoldKey.currentState!.showSnackBar(
SnackBar(
content: Text("Password Is Empty"),
),
);
} else if (password.text.length < 8) {
_scaffoldKey.currentState!.showSnackBar(
SnackBar(
content: Text("Password Is Too Short"),
),
);
} else if (phoneNumber.text.length < 11 || phoneNumber.text.length > 11) {
_scaffoldKey.currentState!.showSnackBar(
SnackBar(
content: Text("Phone Number Must Be 11 "),
),
);
} else if (address.text.isEmpty) {
_scaffoldKey.currentState!.showSnackBar(
SnackBar(
content: Text("Address Is Empty "),
),
);
} else {
submit();
}
}
Widget _buildAllTextFormField() {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
MyTextFormField(
name: "UserName",
controller: userName,
),
SizedBox(
height: 10,
),
MyTextFormField(
name: "Email",
controller: email,
),
SizedBox(
height: 10,
),
PasswordTextFormField(
obserText: obserText,
controller: password,
name: "Password",
onTap: () {
FocusScope.of(context).unfocus();
setState(() {
obserText = !obserText;
});
},
),
SizedBox(
height: 10,
),
GestureDetector(
onTap: () {
setState(() {
isMale = !isMale;
});
},
child: Container(
height: 60,
padding: EdgeInsets.only(left: 10),
width: double.infinity,
decoration: BoxDecoration(border: Border.all(color: Colors.grey)),
child: Center(
child: Row(
children: [
Text(
isMale == true ? "Male" : "Female",
style: TextStyle(color: Colors.black87, fontSize: 18),
),
],
),
),
),
),
SizedBox(
height: 10,
),
MyTextFormField(
name: "Phone Number",
controller: phoneNumber,
),
SizedBox(
height: 10,
),
MyTextFormField(
name: "Address",
controller: address,
),
],
),
);
}
Widget _buildBottomPart() {
return Container(
margin: EdgeInsets.symmetric(horizontal: 10),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
_buildAllTextFormField(),
SizedBox(
height: 10,
),
isLoading == false
? MyButton(
name: "SignUp",
onPressed: () {
vaildation();
},
)
: Center(
child: CircularProgressIndicator(),
),
ChangeScreen(
name: "Login",
whichAccount: "I Have Already An Account!",
onTap: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (ctx) => Login(),
),
);
},
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: ListView(
children: [
Container(
height: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Register",
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.bold,
),
),
],
),
),
Container(
height: 500,
child: _buildBottomPart(),
),
],
),
);
}
}
`