【发布时间】:2021-01-11 08:05:44
【问题描述】:
我是 Flutter 的新手。 我用Android studio试了flutter的教程,报了这个错误。
[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] 未处理异常:PlatformException(ERROR_INVALID_EMAIL, 电子邮件地址格式错误。, null)
我输入的电子邮件地址是真实的。喜欢,erika@gmail.com
为什么会出现这个错误,我该如何解决?
我之前确实把一些包倒过来了,也许是这个原因?
这是我的registrationpage.dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:true_rider/brand_colors.dart';
import 'package:true_rider/screens/loginpage.dart';
import 'package:true_rider/widgets/TaxiButton.dart';
class RegistrationPage extends StatelessWidget {
static const String id = 'register';
final FirebaseAuth _auth = FirebaseAuth.instance;
var fullNameController = TextEditingController();
var phoneController = TextEditingController();
var emailController = TextEditingController();
var passwordController = TextEditingController();
void registerUser() async {
final FirebaseUser user = (await _auth.createUserWithEmailAndPassword(
email: 'emailController.text',
password: 'passwordController.text',
)).user;
if (user != null) {
print('registration successful');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
SizedBox(height: 70,),
Image(
alignment: Alignment.center,
height: 100.0,
width: 100.0,
image: AssetImage('images'
'/logo.png'),
),
SizedBox(height: 40,),
Text('Create a Rider\'s Account',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25, fontFamily: 'Brand-Bold'),
),
Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
TextField(
controller: fullNameController,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: 'Full name',
labelStyle: TextStyle(
fontSize: 14.0,
),
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 10.0,
),
),
style: TextStyle(fontSize: 14),
),
SizedBox(height: 10,),
TextField(
controller: emailController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email address',
labelStyle: TextStyle(
fontSize: 14.0,
),
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 10.0,
),
),
style: TextStyle(fontSize: 14),
),
SizedBox(height: 10,),
TextField(
controller: phoneController,
keyboardType: TextInputType.phone,
decoration: InputDecoration(
labelText: 'Phone number',
labelStyle: TextStyle(
fontSize: 14.0,
),
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 10.0,
),
),
style: TextStyle(fontSize: 14),
),
SizedBox(height: 10,),
TextField(
controller: passwordController,
obscureText: true,
decoration: InputDecoration(
labelText: 'Password',
labelStyle: TextStyle(
fontSize: 14.0,
),
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 10.0,
),
),
style: TextStyle(fontSize: 14),
),
SizedBox(height: 40,),
TaxiButton(
title: 'REGISTER',
color: BrandColors.colorGreen,
onPressed: () {
registerUser();
},
),
],
),
),
FlatButton(
onPressed: (){
Navigator.pushNamedAndRemoveUntil(context, LoginPage.id, (route) => false);
},
child: Text ('Already have a RIDER account? Log in')
),
],
),
),
),
),
);
}
}
这是我的 longinpage.dart
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:true_rider/brand_colors.dart';
import 'package:true_rider/screens/registrationpage.dart';
import 'package:true_rider/widgets/TaxiButton.dart';
class LoginPage extends StatelessWidget {
static const String id = 'login';
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
SizedBox(height: 70,),
Image(
alignment: Alignment.center,
height: 100.0,
width: 100.0,
image: AssetImage('images'
'/logo.png'),
),
SizedBox(height: 40,),
Text('Sign In as a Rider',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 25, fontFamily: 'Brand-Bold'),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
TextField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email address',
labelStyle: TextStyle(
fontSize: 14.0,
),
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 10.0,
),
),
style: TextStyle(fontSize: 14),
),
SizedBox(height: 10,),
TextField(
obscureText: true,
decoration: InputDecoration(
labelText: 'Password',
labelStyle: TextStyle(
fontSize: 14.0,
),
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 10.0,
),
),
style: TextStyle(fontSize: 14),
),
SizedBox(height: 40,),
TaxiButton(
title: 'LOGIN',
color: BrandColors.colorGreen,
onPressed: () {
},
),
],
),
),
FlatButton(
onPressed: (){
Navigator.pushNamedAndRemoveUntil(context, RegistrationPage.id, (route) => false);
},
child: Text ('Don\'t have an account, sign up here')
),
],
),
),
),
),
);
}
}
这是我的 main.dart
import 'package:firebase_core/firebase_core.dart';
import 'package:true_rider/screens/loginpage.dart';
import 'package:true_rider/screens/mainpage.dart';
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:true_rider/screens/registrationpage.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final FirebaseApp app = await FirebaseApp.configure(
name: 'db2',
options: Platform.isIOS || Platform.isMacOS
? const FirebaseOptions(
googleAppID: '1:297855924061:ios:c6de2b69b03a5be8',
gcmSenderID: '297855924061',
databaseURL: 'https://flutterfire-cd2f7.firebaseio.com',
)
: const FirebaseOptions(
googleAppID: '1:17165000282:android:c47ee5756c37488cda4263',
apiKey: 'AIzaSyCTaNcAqsUkUBaqSCAqEMDH4ddweQxwHbc',
databaseURL: 'https://noob-48298.firebaseio.com',
),
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
fontFamily: 'Brand-Regular',
primarySwatch: Colors.blue,
),
initialRoute: RegistrationPage.id,
routes: {
RegistrationPage.id: (context) => RegistrationPage(),
LoginPage.id: (context) => LoginPage(),
MainPage.id: (context) => MainPage(),
},);
}
}
这是我的主页.dart
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
class MainPage extends StatefulWidget {
static const String id = 'mainpage';
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('MainPage'),
),
body: Center(
child: MaterialButton(
onPressed: (){
DatabaseReference dbref = FirebaseDatabase.instance.reference().child('Test');
dbref.set('IsConnected');
},
height: 50,
minWidth: 300,
color: Colors.green,
child: Text('Test Connection'),
),
),
);
}
}
这是我的 TaxiButton.dart
import 'package:flutter/material.dart';
class TaxiButton extends StatelessWidget {
final String title;
final Color color;
final Function onPressed;
TaxiButton({this.title, this.onPressed, this.color});
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: onPressed,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(25)
),
color: color,
textColor: Colors.white,
child: Container(
height: 50,
child: Center(
child: Text(
title,
style: TextStyle(fontSize: 18, fontFamily: 'Brand-Bold'),
),
),
),
);
}
}
这是我的 pubspec.yaml。
name: true_rider
description: A new Flutter application.
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.3
firebase_core: ^0.4.4
firebase_auth: 0.16.1
firebase_database: ^3.1.5
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- images/
fonts:
- family: Brand-Bold
fonts:
- asset: fonts/bolt-semibold.ttf
- family: Brand-Regular
fonts:
- asset: fonts/bolt-regular.ttf
【问题讨论】:
-
'emailController.text.trim()'不是有效的电子邮件地址。 -
谢谢。我删除了修剪,但仍然出现一些错误。
-
'emailController.text'也不是有效的电子邮件地址。引号中的任何内容都是字符串;您正在传递文字文本emailController.text。如果您想将email:参数设置为电子邮件控制器文本的值,则它不应位于字符串中(例如,删除单引号),或者应该是插值字符串(例如,在${})。见dart.dev/guides/language/language-tour#strings -
谢谢。我需要使用 $ 还是 r ??
-
你能给我写一个'emailController.text'正确代码的例子吗?真的很抱歉。
标签: firebase flutter dart firebase-authentication