hi guys I have an error I don't know how to solve it please reply as fast as u can please I'm trying to do the authentication between sign up page and Firebase Auth. this is the sign up page :
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:insight_software/widgets/input_file.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class SignUppage extends StatefulWidget {
SignUppage({key}) : super(key: key);
@override
State<SignUppage> createState() => _SignUppageState();
}
class _SignUppageState extends State<SignUppage> {
var myemail, mypassword, myconPassword;
GlobalKey<FormState> formstate = new GlobalKey<FormState>();
SignUp() async {
var formdata = formstate.currentState;
if (formdata!.validate()) {
print("valid");
formdata.save();
try {
UserCredential userCredential = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: myemail, password: mypassword);
return userCredential;
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
}
} catch (e) {
print(e);
}
} else {}
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: true,
backgroundColor: Colors.white,
appBar: new AppBar(
title: new Text("Sign up"),
backgroundColor: Colors.lightBlue,
),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
height: MediaQuery.of(context).size.height - 200,
width: double.infinity,
child: Form(
key: formstate,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Column(
children: <Widget>[
SizedBox(
height: 20,
),
],
),
inputFile(
label: "Email*",
onSaved: (val) {
myemail = val;
},
validator: (val) {
if (val.length > 100) {
return "email can not be greater than 100 letter";
}
if (val.length < 10) {
return "email can not be less than 10 letter";
}
},
),
inputFile(
label: "Password*",
obscureText: true,
onSaved: (val) {
mypassword = val;
},
validator: (val) {
if (val.length > 40) {
return "password can not be greater than 40 character";
}
if (val.length < 4) {
return "password can not be less than 4 character";
}
},
),
inputFile(
label: "confirm Password*",
obscureText: true,
onSaved: (val) {
myconPassword = val;
},
validator: (val) {
if (val.length > 40) {
return "password can not be greater than 40 character";
}
if (val.length < 4) {
return "password can not be less than 4 character";
}
},
),
//*continue botton
Container(
padding: EdgeInsets.only(top: 3, left: 3),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border(
bottom: BorderSide(color: Colors.black),
top: BorderSide(color: Colors.black),
left: BorderSide(color: Colors.black),
right: BorderSide(color: Colors.black),
)),
child: MaterialButton(
onPressed: () async {
String? response = await SignUp();
if (response != null) {
Navigator.of(context).pushNamed('continue');
} else {
print("sign up failed");
}
},
minWidth: double.infinity,
height: 60,
color: Colors.lightBlue,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
),
child: Text(
"Countinue",
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 18,
color: Colors.white,
),
),
),
),
],
),
),
),
),
);
}
}
and this is the input file :
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
Widget inputFile({label, obscureText = false, validator, onSaved}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
label,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w400,
color: Colors.black,
),
),
SizedBox(
height: 5,
),
// buttton
TextFormField(
obscureText: obscureText,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
),
),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
),
),
),
),
SizedBox(
height: 10,
),
],
);
}
and this is the error faced me
flutter: valid flutter: type 'Null' is not a subtype of type 'String' flutter: sign up failed Reloaded 1 of 631 libraries in 296ms.
CodePudding user response:
insted of var myemail, mypassword, myconPassword; try String ? myemail, mypassword, myconpassword and put a ! operator to avoid null errors at email: myemail!, password: mypassword!
.... still have doubts let me know
CodePudding user response:
I really don't like the key word "var", because with the null safety you can't know what is that. And your probleme comes here. Instead of "var", use String but not String? because that is what your "var" key word is => String? And in your " validator: (val)" check if null return a string.
Where is the call of your function?
void input({required Function onSaved}){
TextFormField(onSaved: (val){
onSaved.call(val);
},
onChanged: (val){
onSaved.call(val);
},
);
}