Flutter身份验证:“电子邮件地址格式错误”问题

时间:2020-04-18 04:28:38

标签: authentication flutter dart

Flutter(Android Studio-Firebase)有人可以解释以下错误的潜在解决方案:电子邮件地址格式错误。我已经尝试了多种实现,但是仍然会遇到此错误(后端抖动可能丢失了某些内容?)。另外,我已经使用简单的testing@gmail.com在Firebase上对该电子邮件进行了身份验证。

谢谢

代码如下:

Main.java

import 'package:firebase_integration/Setup/signIn.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Firebase Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: LoginPage(),
    );
  }
}

signIn.dart

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  String _email, _password;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Sign in'),
      ),
      body: Form(
        key: _formKey,
        child: Column(
          children: <Widget>[
            TextFormField(
              validator: (input) {
                if(input.isEmpty){
                  return 'Please type an email';
                }
              },
              onSaved: (input) => _email = input ,
              decoration: InputDecoration(
                labelText: 'Email'
              ),
            ),
            TextFormField(
              validator: (input) {
                if(input.length < 6){
                  return 'Your password needs to be at least 6 characters';
                }
              },
              onSaved: (input) => _password = input ,
              decoration: InputDecoration(
                labelText: 'Password'
              ),
              obscureText: true,
            ),
            RaisedButton(
              onPressed: signIn,
              child: Text('Sign in'),
            )
          ],
        )
      )
    );
  }

  Future<void> signIn() async {
    final formState = _formKey.currentState;
      if(formState.validate()){
        formState.save();
        try {
          AuthResult user = await FirebaseAuth.instance
            .signInWithEmailAndPassword(
            email: _email,
            password: _password);
          Navigator.push(context, MaterialPageRoute(builder: (context) => Home()));
          // TODO: Navigate to home
        }catch(e){
            print(e.message);
        }

      }
  }
}


class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
    );
  }
}

0 个答案:

没有答案