I am trying to authticate users with firebase, and after the user fills out the form fields, this is how I am sending the user to the next page, after registering the user with my firebase database.
onPressed: () async {
try {
FirebaseUser user =
(await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
))
.user;
if (user != null) {
UserUpdateInfo updateUser = UserUpdateInfo();
updateUser.displayName = _usernameController.text;
user.updateProfile(updateUser);
Navigator.of(context).pushNamed(AppRoutes.Homepage_1);
}
According to docs, the approach above looks correct, but I am getting a number of errors, such as
Undefined class 'FirebaseUser'.
Try changing the name to the name of an existing class, or creating a class with the name 'FirebaseUser'.
Undefined class 'UserUpdateInfo'.
Try changing the name to the name of an existing class, or creating a class with the name 'UserUpdateInfo'.
The method 'UserUpdateInfo' isn't defined for the type '_SignUpViewState'.
Try correcting the name to the name of an existing method, or defining a method named 'UserUpdateInfo'.
These error messages are not making sense as to why they are being thrown, as I have followed the relevant docs, and others online, have used this same code with no problems.
Am I missing something simple, or is something missing entirely from my broader code?
class SignUp extends StatefulWidget {
@override
_SignUpViewState createState() => _SignUpViewState();
}
class _SignUpViewState extends State<SignUp> {
final _formKey = GlobalKey<FormState>();
TextEditingController _firstnameController = TextEditingController();
TextEditingController _emailController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
TextEditingController _repasswordController = TextEditingController();
@override
Widget build(BuildContext context) {
final mq = MediaQuery.of(context);
final logo = Image.asset(
"assets/logo.png",
height: mq.size.height / 4,
);
final firstnameField = TextFormField(
controller: _firstnameController,
style: TextStyle(
color: Colors.white,
),
cursorColor: Colors.white,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
labelText: "First Name",
labelStyle: TextStyle(
color: Colors.white,
),
hintStyle: TextStyle(
color: Colors.white,
),
),
);
final emailField = TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
style: TextStyle(
color: Colors.white,
),
cursorColor: Colors.white,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
labelText: "Email",
labelStyle: TextStyle(
color: Colors.white,
),
hintStyle: TextStyle(
color: Colors.white,
),
),
);
final passwordField = TextFormField(
obscureText: true,
controller: _passwordController,
style: TextStyle(
color: Colors.white,
),
cursorColor: Colors.white,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
labelText: "Password",
labelStyle: TextStyle(
color: Colors.white,
),
hintStyle: TextStyle(
color: Colors.white,
),
),
);
final repasswordField = TextFormField(
obscureText: true,
controller: _repasswordController,
style: TextStyle(
color: Colors.white,
),
cursorColor: Colors.white,
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
hintText: "password",
labelStyle: TextStyle(
color: Colors.white,
),
hintStyle: TextStyle(
color: Colors.white,
),
),
);
final fields = Padding(
padding: EdgeInsets.only(top: 10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
firstnameField,
emailField,
passwordField,
repasswordField,
],
),
);
final registerButton = Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(25.0),
color: Colors.white,
child: MaterialButton(
minWidth: mq.size.width / 1.2,
padding: EdgeInsets.fromLTRB(10.0, 15.0, 10.0, 15.0),
child: Text(
"Register",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
onPressed: () async {
try {
FirebaseUser user =
(await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
))
.user;
if (user != null) {
UserUpdateInfo updateUser = UserUpdateInfo();
updateUser.displayName = _firstnameController.text;
user.updateProfile(updateUser);
Navigator.of(context).pushNamed(AppRoutes.Homepage_1);
}
} catch (e) {
print(e);
_firstnameController.text =
""; //these reset the responses when there is an error in the form;LOOK HERE
_passwordController.text = "";
_repasswordController.text = "";
_emailController.text = "";
// TODO: alertdialog with error
}
},
),
);
final bottom = Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
registerButton,
Padding(
padding: EdgeInsets.all(8.0),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Already have an account?",
style: Theme.of(context).textTheme.subtitle1.copyWith(
color: Colors.white,
),
),
MaterialButton(
onPressed: () {
Navigator.of(context).pushNamed(AppRoutes.authLogin);
},
child: Text(
"Login",
style: Theme.of(context).textTheme.subtitle1.copyWith(
color: Colors.white,
decoration: TextDecoration.underline,
),
),
),
],
),
],
);
return Scaffold(
backgroundColor: Color(0xff8c52ff),
body: Form(
key: _formKey,
child: SingleChildScrollView(
padding: EdgeInsets.all(36),
child: Container(
height: mq.size.height,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
logo,
fields,
Padding(
padding: EdgeInsets.only(bottom: 150),
child: bottom,
),
],
),
),
),
),
);
}
}