gpt4 book ai didi

enums - Dart枚举编译器错误

转载 作者:行者123 更新时间:2023-12-02 10:42:15 25 4
gpt4 key购买 nike

我正在尝试在Dart中定义以下“枚举”:

class Place {
static const SigninPlace = const Place._("", new Map<String,String> { "fizz": "buzz"});
static const SignoutPlace = const Place._("", null);

static get values => [SigninPlace, SignoutPlace];

final String name;
final Map<String,String> params;

const Place._(this.name, this.params);
}

但是在 SigninPlace声明中出现编译器错误:

Arguments of a constant creation must be constant expressions



为什么?该怎么解决? (提示:每个 Place<String,String>映射不会改变,并且在启动时就已知)。

最佳答案

当您定义const时,其所有成员都必须是常量表达式。在您的情况下,new Map<String,String> { "fizz": "buzz"}不是常量表达式。您必须使用const <String,String>{ "fizz": "buzz"}创建一个常量Map。

class Place {
static const SigninPlace = const Place._("",
const <String,String>{ "fizz": "buzz"});
static const SignoutPlace = const Place._("", null);

static get values => [SigninPlace, SignoutPlace];

final String name;
final Map<String,String> params;

const Place._(this.name, this.params);
}

关于enums - Dart枚举编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20859226/

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