This is my code .. And this is running successfully there are no errors but once i clicked on Sign-up button its throws the error.
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MaterialApp(
home: Homepage(),
routes: {
'/home': (context) {
return Homepage();
},
'/first': (context) {
return First_Page();
},
},
));
}
class Homepage extends StatefulWidget {
const Homepage({Key? key}) : super(key: key);
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
final _auth = FirebaseAuth.instance;
final _user = TextEditingController();
final _pass = TextEditingController();
String email = '';
String password = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text(
'Sign-Up',
),
centerTitle: true,
),
body: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: EdgeInsets.only(top: 250),
),
Container(
child: TextFormField(
controller: _user,
decoration: InputDecoration(
hintText: "Email-id",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
)),
),
),
SizedBox(
height: 10,
),
Container(
child: TextFormField(
controller: _pass,
decoration: InputDecoration(
hintText: "Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
)),
),
),
FlatButton(
color: Colors.blue,
child: Text('Sign-up'),
onPressed: () async {
await Firebase.initializeApp();
email = _user.toString();
password = _pass.toString();
final newuser = await _auth.createUserWithEmailAndPassword(
email: email.trim(),
password: password.trim(),
);
if (newuser != null) {
Navigator.pushNamed(context, '/First_Page');
} else
print('Error');
},
),
],
),
),
);
}
}
These are the all errors that i am getting
E/flutter (10268): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception:
[firebase_auth/invalid-email] The email address is badly formatted.
E/flutter (10268): #0 MethodChannelFirebaseAuth.createUserWithEmailAndPassword
(package:firebase_auth_platform_interface/src/method_channel
/method_channel_firebase_auth.dart:272:7)
E/flutter (10268): <asynchronous suspension>
E/flutter (10268): #1 FirebaseAuth.createUserWithEmailAndPassword
(package:firebase_auth/src/firebase_auth.dart:238:7)
E/flutter (10268): <asynchronous suspension>
E/flutter (10268): #2 _HomepageState.build.<anonymous closure>
(package:firebase/main.dart:79:31)
E/flutter (10268): <asynchronous suspension>
E/flutter (10268):
And in firebase authentication I already enabled the email sign in method, now i am not getting where i mistaken. plz tell me .Thanks in advance..
CodePudding user response:
For the email
address you're using:
email=_user.toString();
With _user
being:
final _user=TextEditingController()
So you're calling toString
on a TextEditingController
, which gives you a debug representation of that controller. If you want the value from that controller, you'll want to use:
email=_user.value;
To spot this type of problem yourself, I recommend learning to set breakpoints and use a debugger. If you set a breakpoint on the _auth.createUserWithEmailAndPassword
(which is where the exception comes from) and run in the debugger, you can easily see that email
does not have the value you expect. From there it should be a matter of tracing backwards to where the value comes from, and looking at the documentation for TextEditingController
to see the correct call.