gpt4 book ai didi

dart - 在 Dart 中,是否有可能有闭包的 const 映射?

转载 作者:行者123 更新时间:2023-12-04 13:26:32 25 4
gpt4 key购买 nike

我创建了一个闭包的文字映射,例如:

Map<String, Function> mapOfFuncs = {
'foo': (a, b, c) => ... ,
'bar': (a, b, c) => ... ,
...
}

到目前为止一切都很好。然后我想做这张 map const ,因为它在我的程序中是全局的,不应该被修改。
const Map<String, Function> MAP_OF_FUNCS = const {
'foo': (a, b, c) => ... ,
'bar': (a, b, c) => ... ,
...
}

由于 map 中的文字闭包不是 const .

在 Dartpad 上: https://dartpad.dartlang.org/817d2cfd141b0a56fc7d

我会认为文字闭包是 const .有没有办法让他们这样?

最佳答案

const 表达式不支持闭包。有一个 Unresolved 问题 https://github.com/dart-lang/sdk/issues/4596https://github.com/Pajn/dep-const-function-literals/issues/1

如果您改为创建静态函数,您可以在 const 映射文字中引用它们,但您目前无法内联定义它们。

关于dart - 在 Dart 中,是否有可能有闭包的 const 映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35537389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com