【问题标题】:why firebase auth doesn't work on my new samsung phone?为什么 firebase auth 在我的新三星手机上不起作用?
【发布时间】:2021-10-28 11:49:41
【问题描述】:

我是 flutter 的初学者。 我有一个问题 firebase 身份验证,可以在所有手机和模拟器上使用电子邮件,但在我的新三星手机上却没有。 这是错误信息

/FirebaseAuth( 2571): [FirebaseAuth:] Preparing to create service connection to fallback implementation
W/System  ( 2571): Ignoring header X-Firebase-Locale because its value was null.
2
I/System.out( 2571): (HTTPLog)-Static: isSBSettingEnabled false
D/InputMethodManager( 2571): HSIFW - flag : 0

这是用户注册或登录的身份验证屏幕。

 import 'package:flutter/material.dart';
   import 'package:m51/common/constants.dart';
  import 'package:m51/common/loading.dart';
  import 'package:m51/services/authentication.dart';

class AuthenticateScreen extends StatefulWidget {
  @override
  _AuthenticateScreenState createState() => _AuthenticateScreenState();
}

class _AuthenticateScreenState extends State<AuthenticateScreen> {
  final AuthenticationService _auth = AuthenticationService();
  final _formKey = GlobalKey<FormState>();
  String error = '';
  bool loading = false;

  final emailController = TextEditingController();
  final nameController = TextEditingController();
  final passwordController = TextEditingController();
  bool showSignIn = true;

  @override
  void dispose() {
    nameController.dispose();
    emailController.dispose();
    passwordController.dispose();
    super.dispose();
  }

  void toggleView() {
    setState(() {
      _formKey.currentState?.reset();
      error = '';
      emailController.text = '';
      nameController.text = '';
      passwordController.text = '';
      showSignIn = !showSignIn;
    });
  }

  @override
  Widget build(BuildContext context) {
    return loading
        ? Loading()
        : Scaffold(
            backgroundColor: Colors.white,
            appBar: AppBar(
              backgroundColor: Colors.blueGrey,
              elevation: 0.0,
              title: Text(showSignIn ? 'Sign in to Water Social' : 'Register to Water Social'),
              actions: <Widget>[
                TextButton.icon(
                  icon: Icon(
                    Icons.person,
                    color: Colors.white,
                  ),
                  label: Text(showSignIn ? "Register" : 'Sign In',
                      style: TextStyle(color: Colors.white)),
                  onPressed: () => toggleView(),
                ),
              ],
            ),
            body: Container(
              padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 30.0),
              child: Form(
                key: _formKey,
                child: Column(
                  children: [
                    !showSignIn
                        ? TextFormField(
                            controller: nameController,
                            decoration: textInputDecoration.copyWith(hintText: 'name'),
                            validator: (value) =>
                                value == null || value.isEmpty ? "Enter a name" : null,
                          )
                        : Container(),
                    !showSignIn ? SizedBox(height: 10.0) : Container(),
                    TextFormField(
                      controller: emailController,
                      decoration: textInputDecoration.copyWith(hintText: 'email'),
                      validator: (value) =>
                          value == null || value.isEmpty ? "Enter an email" : null,
                    ),
                    SizedBox(height: 10.0),
                    TextFormField(
                      controller: passwordController,
                      decoration: textInputDecoration.copyWith(hintText: 'password'),
                      obscureText: true,
                      validator: (value) => value != null && value.length < 6
                          ? "Enter a password with at least 6 characters"
                          : null,
                    ),
                    SizedBox(height: 10.0),
                    ElevatedButton(
                      child: Text(
                        showSignIn ? "Sign In" : "Register",
                        style: TextStyle(color: Colors.white),
                      ),
                      onPressed: () async {
                        if (_formKey.currentState?.validate() == true) {
                          setState(() => loading = true);
                          var password = passwordController.value.text;
                          var email = emailController.value.text;
                          var name = nameController.value.text;

                          dynamic result = showSignIn
                              ? await _auth.signInWithEmailAndPassword(email, password)
                              : await _auth.registerWithEmailAndPassword(name, email, password);
                          if (result == null) {
                            
                             print ('Please supply a valid email');
                            }
                          }
                        }
                      
                    ),
                    SizedBox(height: 10.0),
                    Text(
                      error,
                      style: TextStyle(color: Colors.red, fontSize: 15.0),
                    )
                  ],
                ),
              ),
            ),
          );
  }
}

我也有这个消息

I/flutter (4866): [firebase_auth/invalid-email] 电子邮件地址是 格式错误。

服务代码

import 'package:firebase_auth/firebase_auth.dart';
import 'package:urjob/models/user.dart';
import 'package:urjob/services/database.dart';

class AuthenticationService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  AppUser? _userFromFirebaseUser(User? user) {
    return user != null ? AppUser(user.uid) : null;
  }

  Stream<AppUser?> get user {
    return _auth.authStateChanges().map(_userFromFirebaseUser);
  }

  Future signInWithEmailAndPassword(String email, String password) async {
    try {
      UserCredential result =
          await _auth.signInWithEmailAndPassword(email: email, password: password);
      User? user = result.user;
      return _userFromFirebaseUser(user);
    } catch (exception) {
      print(exception.toString());
      return null;
    }
  }

  Future registerWithEmailAndPassword(String name, String email, String password, String profilepic) async {
    try {
      UserCredential result =
          await _auth.createUserWithEmailAndPassword(email: email, password: password);
      User? user = result.user;
      if (user == null) {
        throw Exception("No user found");
      } else {
        await DatabaseService(user.uid).saveUser(name,profilepic,email);

        return _userFromFirebaseUser(user);
      }
    } catch (exception) {
      print(exception.toString());
      return null;
    }
  }

  Future signOut() async {
    try {
      return await _auth.signOut();
    } catch (exception) {
      print(exception.toString());
      return null;
    } 
  }
Future sendreset(String email) async {
    try {
      return await _auth.sendPasswordResetEmail(email: email);
    } catch (exception) {
      print(exception.toString());
      return null;
    } 
  }
 

}

感谢您的帮助

【问题讨论】:

  • 您似乎已将 Firebase API 调用封装在 AuthenticationService 中,这使我们无法准确说出您做错了什么。鉴于错误消息 The email address is badly formatted.,我会检查该服务并查看您传递给 Firebase API 的确切值。

标签: android flutter firebase-authentication


【解决方案1】:

我找到了解决方案 我们需要在 ma case 中添加 .trim() 函数来擦除空格 https://github.com/FirebaseExtended/flutterfire/issues/1760

【讨论】:

    【解决方案2】:

    修剪从文本字段中获取的文本。

    emailController.text.trim(), 
    passwordController.text.trim()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      • 2018-03-20
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多