有没有办法在 Flutter 的小部件内使用 catch 块的结果

大卫

我正在构建一个以 Firebase 作为后端的颤振应用程序。我在一个单独的文件上创建了一个 AuthService 类,并在登录屏幕中导入和使用 Auth 函数。

这是我的 AuthService 类。

class AuthService {
  Future<UserModel?> signInWithEmailAndPassword(
      String email, String password) async {
    try {
      final cred = await _auth.signInWithEmailAndPassword(
          email: email, password: password);

      return _userFromFirebase(cred.user);
    } on auth.FirebaseAuthException catch (e) {
      print(e.toString());
      return null;
    }
  }
}

在登录页面中,我初始化了函数:

    final auth = Provider.of<AuthService>(context);

然后在 onPressed 中使用它:

                          press: () async {

                            // SIGN IN WITH EMAIL AND PASSWORD
                            dynamic result =
                                await auth.signInWithEmailAndPassword(
                                    email, password);

                            // IF SIGN IN FAILS
                            if (result == null) {
                              setState(() {
                                errorSigningIn = 'Sign in error';
                                //this is where I want to use the error response. 
                              });
                            }
                          },

我坚持使用我在 signInWithEmailAndPassword 函数中捕获的错误并将其分配给 SignIn 小部件中的 errorSigningIn 变量。

我是新手,请帮忙。

谢谢。

电子

您可以创建自己的类来处理身份验证结果。例如:

class AuthResult {
    final int code;
    final UserModel? user;
    final String? errorMessage;

    AuthResult(this.code, {
        this.user,
        this.errorMessage,
    });
}

此类可以帮助您处理所有登录情况。这是您应该使用登录方法执行的操作:

class AuthService {
  Future<AuthResult> signInWithEmailAndPassword(
      String email, String password) async {
    try {
      final cred = await _auth.signInWithEmailAndPassword(
          email: email, password: password);

      return AuthResult(200, user: _userFromFirebase(cred.user));
    } on auth.FirebaseAuthException catch (e) {
      print(e.toString());
      return AuthResult(0 /*<-- your error result code*/, e.toString());
    }
  }
}

最后,您的onPressed

                          press: () async {

                            // SIGN IN WITH EMAIL AND PASSWORD
                            AuthResult result =
                                await auth.signInWithEmailAndPassword(
                                    email, password);

                            // IF SIGN IN FAILS
                            if (result.code != 200) {
                              setState(() {
                                errorSigningIn = result.errorMessage; //<-- Get your error message
                                //this is where I want to use the error response. 
                              });
                            }
                          },

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有办法做反Sprintf结果

有没有办法使HTML可重用块?

有没有办法限制/页面结果集

Flutter:在有状态小部件内使用有状态小部件数组会导致“ RenderFlex溢出”

有没有办法在Flutter中使用PageView进行无限循环?

有没有办法在Flutter中的多个PageRoutes中使用InheritedWidget?

有没有办法知道我的flutter应用程序使用哪个Android API版本?

有没有办法使用sqflite在flutter应用程序中检查数据库版本?

有没有办法将图像从本地存储加载到Flutter中的CircleAvatar小部件的backgroundImage属性?

有没有办法在catch块中编写if语句?

有没有办法使用Evernote突出显示语法或代码块?

有没有办法使用R读取Qlikview数据对象并执行统计操作并将结果返回给Qlikview?

有没有办法在 DIV 块中使用两个样式条件

有没有办法在不使用 Flutter 中的 Mock 类扩展的情况下模拟对象?

使用 Google Script,有没有办法将运行函数的结果导出到新工作表中?

有没有办法在 vscode/visual studio code flutter 中自动填充所需的类/小部件属性?

有没有办法使用类似地图的语法在 Scala 的非地图代码块中设置变量?

有没有办法在flutter项目中使用libVLC?

有没有办法在flutter中使用CupertinoAlertDialog创建pop Modal?

有没有办法在打印结果时丢弃使用 malloc/realloc 存储的数字?

有没有办法使用flutter防止IOS中的屏幕截图?

有没有办法使用 Python 将从 bigquery 返回的结果转换为 Json 格式?

有没有办法在函数内使用 bgcolor ?

有没有办法将 shared_preferences 与 country_list_pick (Flutter) 一起使用?

有没有办法使用android studio在我的flutter项目中创建一个Podfile?

有没有办法使用 Dart/Flutter 访问 ForEach 中的索引/键

有没有办法在 Groovy 中的导入周围放置一个 try/catch 块?

有没有办法检测移动浏览器中使用的flutter web?

有没有办法使用 Tweepy 从 search_tweets 的结果中过滤掉转推?