首页 > 解决方案 > ERROR_INVALID_EMAIL,电子邮件地址格式错误。,null

问题描述

我是 Flutter 的新手。我尝试了使用 Android Studio 的颤振教程并得到了这个错误。

[错误: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

标签: firebaseflutterdartfirebase-authentication

解决方案


您正在尝试使用以下方式创建用户:

_auth.createUserWithEmailAndPassword(
  email: 'emailController.text',
  password: 'passwordController.text',
)

由于'emailController.text'and'passwordController.text'在它们周围有引号,它们是文字字符串。因此,这些文字字符串会传递给 Firebase 身份验证,而第一个字符串不是有效的电子邮件地址。

您可能希望使用用户在这些控件中键入的值,在这种情况下,您应该删除引号:

_auth.createUserWithEmailAndPassword(
  email: emailController.text,
  password: passwordController.text,
)

推荐阅读