【问题标题】:How to work around 'Future<dynamic>' is not a subtype of type 'FirebaseUser' error如何解决“Future<dynamic>”不是“FirebaseUser”错误类型的子类型
【发布时间】:2020-08-19 20:17:27
【问题描述】:

我有一个 FirebaseActions 类,它正在为我进行注册和登录工作。我在其中添加了一个 getCurrentUser 方法。我正在从我的 HomeScreen 小部件中调用该函数。我需要将返回的值(type= FirebaseUser)放入我的 HomeScreen 中的一个变量中以访问loggedInUser.email。但我得到这个错误。我的问题;有什么方法可以将 FirebaseUser 类型的数据放入 Future 类型变量中?当我在 HomeScreen 而不是 FirebasAction 类中编写此函数时,它可以工作,但这是最佳实践吗?

FirebaseActions 类

static getCurrentUser(context) async {
    final user = await _auth.currentUser().catchError((error) {
      Scaffold.of(context).showSnackBar(AppSnackBar.appSnackBar(error.message));
    });

    if (user != null) {
      return user;
    }
  }

主屏幕小部件

class _HomeScreenState extends State<HomeScreen> {

  FirebaseUser loggedInUser;

  @override
  void initState() {
    super.initState();
    loggedInUser = FirebaseActions.getCurrentUser(context);
    print(loggedInUser.toString());
  }

【问题讨论】:

    标签: firebase flutter future


    【解决方案1】:

    您收到此错误是因为您没有在 getCurrentUser() 函数中指定返回类型。

    用下面的代码替换你的代码,一切都会正常。

    要解决这个问题: 检查下面的代码:它工作得很好:

    Firebase 操作类

    // give your function a return type
    static Future<FirebaseUser> getCurrentUser(context) async {
        final user = await _auth.currentUser().catchError((error) {
          Scaffold.of(context).showSnackBar(AppSnackBar.appSnackBar(error.message));
        });
    
        if (user != null) {
          return user;
        }
      }
    

    主屏幕小部件

    class _HomeScreenState extends State<HomeScreen> {
    
      FirebaseUser loggedInUser;
    
      @override
      void initState() {
        super.initState();
        call the function here
        logInUser();
        print(loggedInUser.toString());
      }
    

    希望对你有帮助

    更新

    // create a new function to log in user
    void logInUser() async {
     // await the result because you are invoking a future method
        loggedInUser = await FirebaseActions.getCurrentUser(context);
    }
    

    【讨论】:

    • 问题是;当我尝试使用 await 获取 loggedInUser 时,它显示“await 只能在异步函数中使用”。但是我不能这样做,因为 initState 不能是异步的。我该怎么办?
    • 创建函数名logInUser并在initState中调用。检查我的代码。我更新了。
    • 是的,当你写这个答案时,我正在这样做。非常感谢。
    • 很高兴它帮助了你!
    【解决方案2】:

    你没有指定方法的返回类型,而且方法是异步的,所以你必须把等待你调用的地方。

     static Future<FirebaseUser> getCurrentUser(context) async {
    

    另外,在你调用的地方添加 await。

    loggedInUser = await FirebaseActions.getCurrentUser(context);
    

    更新:

    创建新函数并调用该函数。

    callme() async{  
     loggedInUser = FirebaseActions.getCurrentUser(context);
        print(loggedInUser.toString());
    }
    

    初始化状态:

     @override
      void initState() {
        super.initState();
        callme();
      }
    

    【讨论】:

    • 问题是;当我尝试使用 await 获取 loggedInUser 时,它显示“await 只能在异步函数中使用”。但是我不能这样做,因为 initState 不能是异步的。我该怎么办?
    【解决方案3】:

    这是我在 initstate 中处理异步函数的方式:

    FirebaseActions.getCurrentUser(context).then((val){loggedInUser = val});
    

    还要确保在异步函数中指定返回类型。

    Future<FirebaseUser> getCurrentUser(context) async {...
    

    【讨论】:

    • 用回调函数解决这个问题是另一个不错的选择。谢谢。
    猜你喜欢
    • 2021-08-13
    • 2022-01-23
    • 2020-11-28
    • 2020-01-01
    • 2020-01-07
    • 2021-02-17
    • 2019-06-07
    • 2020-09-24
    • 2020-09-11
    相关资源
    最近更新 更多