【问题标题】:is this possible to use Map<String, Function> in flutter?这可以在颤振中使用 Map<String, Function> 吗?
【发布时间】:2022-11-10 03:07:41
【问题描述】:

我是 Flutter 的新手。 我正在尝试使用这句话

  Map<String, Function> map = Map();

  Function fun1 = (() => print("TAG : 1111"));
  map.putIfAbsent("test", fun1);
  map["test"]!.call();

但是在运行时,我遇到了这样的错误。

Dart Unhandled Exception: type '_TypeError'

由于不支持反射,我想使用包含某些功能的地图 我的情况有什么解决方案吗?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    将您的 map.putIfAbsent() 更新为:

    map.putIfAbsent("test", () => fun1);
    

    【讨论】:

    • 添加到评论中,fun1 的类型应该是 Null Function() 否则你会得到 The return type 'Function' isn't a 'Null Function()', as required by the closure's context.
    【解决方案2】:
    Map<String, void Function()> mapName = {
        "some string": () {
            // function code
        },
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2020-08-02
      • 2021-12-25
      • 2021-08-07
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多