为什么我的 flutter firebase 身份验证有问题?

弗洛米特里

我的目标是使用我的 firebase 数据上的现有帐户登录,并在该帐户不存在时显示异常......我遵循了一些教程以及官方 firebase 和 flutter 文档。当我尝试使用不存在或现有的帐户登录时,在主屏幕中导航的按钮登录不起作用。

另外我有这个错误信息 type 'String' is not a subtype of type 'TextEditingController' in type cast

我有这个功能的两页代码

首先是我在 auth Provider Service Screen 中的代码:

import 'package:firebase_auth/firebase_auth.dart';

class AuthClass {
  FirebaseAuth auth = FirebaseAuth.instance;

  Future<String?> signIN({required String email, required String password}) async {
    try {
      await auth.signInWithEmailAndPassword(
          email: email,
          password: password,);
      return "Welcome";
    } on FirebaseAuthException catch (e) {
      if (e.code == 'user-not-found') {
        return 'No user found for that email.';
      } else if (e.code == 'wrong-password') {
        return 'Wrong password provided for that user.';
      }
    }
  }
}

这是我的身体登录屏幕代码:

import 'package:yona/WelcomePage/WelcomePage.dart';

class Body extends StatefulWidget {
  const Body({
    Key? key,
  }) : super(key: key);

  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  final GlobalKey<FormState> _formKey  = GlobalKey<FormState>();
  final auth = FirebaseAuth.instance;
  TextEditingController _email = TextEditingController();
  TextEditingController _password = TextEditingController();


  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery
        .of(context)
        .size;
    return Background(
        child: Form(
        key: _formKey,
        child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
        Text("LOGIN", style: TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.bold,
            fontSize: 20)),
          Container(child: Image.asset('assets/ctlogo.png', height: 200),
          ),
       // Container
        RoundedInputField(
            hintText: "Your Email",
            onChanged: (value) {
              setState(() {
                _email = value.trim() as TextEditingController;
              });
            }
        ),
        RoundedPasswordField(
          onChanged: (value) {
            setState(() {
              _password = value.trim() as TextEditingController;
            });
          },
        ),
        Container(margin: EdgeInsets.symmetric(vertical: 20),
          width: 300,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(29),
            // ignore: deprecated_member_use
            child: FlatButton(
              padding: EdgeInsets.symmetric(vertical: 14, horizontal: 40),
              color: SandYellow,
              onPressed: () {
                AuthClass()
                    .signIN(
                    email: _email.text.trim(), password: _password.text.trim())
                    .then((value) {
                  if (value == 'Welcome') {
                    Navigator.pushAndRemoveUntil(
                      context,
                      MaterialPageRoute(
                        builder: (context) => HomeScreen()),
                      (route) => false);
                    }
                  else{
                    ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(value!)));
                  }
                  }
                );
              },
              child: Text("LOGIN",
                  style: TextStyle(color: DarkTurquoise,
                    fontSize: 16,)
              ),
            ),
          ),
        ),
埃姆雷·阿卡达格
value.trim() as TextEditingController;

value 已经是 String,因此您不能将类型转换为 TextEditingController。_email 变量应该是这样的字符串。

你可以删除as TextEditingController这部分。所以它会起作用。

编辑:

TextEditingController _email = TextEditingController();
TextEditingController _password = TextEditingController();

这些变量不应该是控制器,应该是字符串。

String _email = '';
String _password = '';

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我正在使用 firebase flutter 实现电话身份验证。但是有个问题

Flutter Firebase身份验证

我将Firebase规则更改为所有经过身份验证的用户。.我需要更改请求的内容(Flutter)

Flutter Firebase身份验证注销活动

带身份验证的 Flutter Firebase 上传

Firebase 获取身份验证信息 Flutter

这段代码有什么作用?角度 Firebase 身份验证

我需要使用 flutter 和 firebase 添加电子邮件/密码身份验证

Firebase for Flutter中此身份验证背后的逻辑是什么?

Flutter Firebase电子邮件和密码身份验证登录时遇到问题

为什么我的快速代码无法正常执行?Firebase身份验证

Flutter Firebase:如何查找所有身份验证错误代码?

在具有Flutter和Firebase身份验证的设备上坚持用户

Flutter Firebase 身份验证仅在您提供测试电话号码时有效

带有Firebase身份验证的Flutter Web开发人员:MissingPluginException

为什么 firebase 身份验证返回 null?

为什么Android Firebase跳过身份验证活动

Firebase 身份验证 - 没有登录问题

从Flutter设置Firebase身份验证的自定义声明

Flutter - 无法使用 Firebase 身份验证登录

如何使用Flutter和Firebase进行离线身份验证

Flutter Firebase身份验证:恢复为特定的匿名用户

如何处理flutter中的firebase身份验证异常?

Flutter Web上无法持久保留Firebase身份验证

如何在Flutter中管理Firebase身份验证状态?

如何使用Flutter增加Firebase身份验证会话时间?

Flutter中的Firebase身份验证状态检查

如何使用Firebase身份验证在Flutter中注销用户

如何使用Firebase在Flutter中进行电话身份验证?