简体   繁体   English

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

[英]Why am I having issues with my flutter firebase authentification?

My goal is to sign in with an existing account on my firebase data and show exceptions when the account is not existing... I followed some tutorials and the official firebase and flutter documentation.我的目标是在我的 firebase 数据上使用现有帐户登录,并在帐户不存在时显示异常...我遵循了一些教程以及官方 firebase 和 flutter 文档。 When I'm trying to log with an not existing or existing account the button login to naviguate in the home screen doesn't work.当我尝试使用不存在或现有的帐户登录时,用于在主屏幕中导航的登录按钮不起作用。

Plus I have this error message type 'String' is not a subtype of type 'TextEditingController' in type cast另外我有这个错误消息type 'String' is not a subtype of type 'TextEditingController' in type cast

I have two pages code for this function我有这个 function 的两页代码

First my code in auth Provider Service Screen:首先是我在 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.';
      }
    }
  }
}

and this is my body login screen code:这是我的身体登录屏幕代码:

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 is already String so you can not type cast to TextEditingController. value 已经是 String,因此您不能将类型强制转换为 TextEditingController。 _email variable should be a String in that way. _email 变量应该是这样的字符串。

You can just delete as TextEditingController this part.您可以将这部分as TextEditingController删除。 so It will be worked.所以它会起作用。

Edit:编辑:

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

these variables should not be a Controller, should be a String.这些变量不应该是Controller,应该是String。

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

No need to setState when changing TextEditingController value.更改setState值时无需设置TextEditingController

onChanged: (value) {
  _email.text = value.trim();
}),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我无法将我的 flutter iOS 应用程序添加到我的 firebase 项目中? - Why am I unable to add my flutter iOS app to my firebase project? 与提供商 flutter 的身份验证 Firebase - authentification Firebase with Provider flutter 我在运行我的应用程序时收到此 E/flutter (25055) 错误(Flutter,FireBase - I am getting this E/flutter (25055) error when I am running my App (Flutter , FireBase 为什么我在 iOS(颤振应用程序)中收到 Firebase AppCheck 错误 - Why am I getting a Firebase AppCheck error in iOS (Flutter app) 我遇到 SQL 查询问题 - I am having issues with a SQL query 我正在尝试从我的 flutter 移动应用程序中调用 firebase 云功能。 我可以从 firebase 函数调用那些:shell 或本地 - I am trying to call firebase cloud functions from my flutter mobile application. I am able to call those from firebase functions : shell or locally 使用我的 email 和密码注册 Firebase 身份验证后,我遇到了很多问题。 当我使用那个方法时, - I'm having a lot of issues after signing up for Firebase auth with my email and password. When I use that method, .then((userCredential) => { } 不使用 firebase 身份验证 - .then((userCredential) => { } not working with firebase authentification 我是 flutter 的新手,有人知道如何连接 flutter 注册 firebase 吗? - I am new to flutter, does anyone knows how to connect flutter sign up with firebase? 为什么我在连接 Firebase 时遇到 Arduino 中的这个问题? - Why am I facing this Problem in Arduino when connecting Firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM