首页 > 解决方案 > 未来' 不是 '(() => dynamic) 类型的子类型?

问题描述

当我尝试延迟保存令牌设备的方法时出现此错误。

    final pushnoti = new PushNoti();
    pushnoti.configurePushNoti(context);
    Future.delayed(Duration(seconds: 3), pushnoti.saveTokenDevice(currentUid));
    return StreamBuilder<UserData>()

这是方法定义:

  saveTokenDevice(String currentUid) async {
    tokenDevice = await _firebaseMessaging.getToken();
    if (tokenDevice != null) {
      var tokenRef =  _database
        .collection(user_database)
        .document(currentUid);

      if (tokenRef != null){
      await tokenRef.updateData({
        'TokenDevice': FieldValue.arrayUnion([tokenDevice])
      }
      );
      }
      else {
      }
    }

我想做的是在创建用户后延迟该方法。

标签: flutter

解决方案


Thewdelayed构造函数需要一个返回值或 a 的函数Future,但您编写代码的方式是saveTokenDevice函数的结果,即 a Future。应该是这样的:

Future.delayed(Duration(seconds: 3), () => pushnoti.saveTokenDevice(currentUid));

来源:https ://api.dart.dev/stable/2.10.1/dart-async/Future/Future.delayed.html


推荐阅读