Dart中的字符串文字类型(如TypeScript中的字符串)? [英] String Literal Types (like in TypeScript) in Dart?
问题描述
由于Flutter的帮助,我开始使用Dart。
我以前使用TypeScript,它提供了一些以前从未见过的非常酷的功能。 ,我特别喜欢的是字符串文字类型,看起来像此
type缓和=缓入 | 缓解 | 由内而外;
函数doSomething(easing:Easing){/ *做某事* /}
doSomething( ease-in); // OK
doSomething( easy); //错误
在Dart中,我发现使用 一个示例来自 Dart中是否有一种方法可以使这些魔术字符串像在TypeScript的字符串文字类型中一样? 每个 然后 I started using Dart thanks to Flutter and I quite like the language. I was using TypeScript before which offered some really cool features that I hadn't seen before, one I particularly liked is the string literal types that look something like this In Dart, I find using One example of this would be from the Is there a way in Dart to have these "magic strings" like in TypeScript's string literal types? Each then
这篇关于Dart中的字符串文字类型(如TypeScript中的字符串)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!枚举$ c $有时会带来不便,尤其是在与特定于平台的插件实现交互时。每当我需要为Android的
静态字符串
s或枚举
s添加Dart接口时,我都会想念字符串文字类型。 / p>
android_intent
插件(但更常见):
void _createAlarm(){
final AndroidIntent intent = const AndroidIntent(
action:'android.intent.action.SET_ALARM',
arguments:< String,dynamic> {
'android.intent.extra.alarm .DAYS':< int> [2,3,4,5,6],
'android.intent.extra.alarm.HOUR':21,
'android.intent.extra.alarm .MINUTES':30,
'android.intent.extra.alarm.SKIP_UI':真,
'android.intent.extra.alarm.MESSAGE':'创建Flutter应用程序',
},
);
intent.launch();
}
枚举
常量会创建 Enum $ $具有
实例,该方法将返回名称为 toString()
方法的c $ c> enum $ c的字符串$ c>类型和常量的名称。例如,给定:
枚举MyEnum {
myConstant,
}
MyEnum.myConstant.toString()
将返回 MyEnum.myConstant
。尽管这不能让您轻松创建任意字符串,但是您可以执行以下操作:
枚举警报{
DAYS ,
HOUR,
MINUTES,
SKIP_UI,
MESSAGE,
}
void _createAlarm(){
最终参数= < alarm,dynamic> {
警报。DAYS:< int> [2、3、4、5、6],
警报。小时:21,
警报。分钟:30 ,
警报。SKIP_UI:true,
警报。消息:创建Flutter应用,
};
最终的AndroidIntent目的= const AndroidIntent(
action:'android.intent.action.SET_ALARM',
arguments:arguments.map((k,v)=> MapEntry ('android.intent.extra。$ k',v)),
);
intent.launch();
}
type Easing = "ease-in" | "ease-out" | "ease-in-out";
function doSomething(easing: Easing) { /* do something */}
doSomething("ease-in"); // OK
doSomething("easy"); // ERROR
enum
s sometimes inconvenient, especially when interacting with platform-specific implementations of plugins. I miss string literal types every time I need to add a Dart interface for Android's static String
s or enum
s.android_intent
plugin (but happens much more often):void _createAlarm() {
final AndroidIntent intent = const AndroidIntent(
action: 'android.intent.action.SET_ALARM',
arguments: <String, dynamic>{
'android.intent.extra.alarm.DAYS': <int>[2, 3, 4, 5, 6],
'android.intent.extra.alarm.HOUR': 21,
'android.intent.extra.alarm.MINUTES': 30,
'android.intent.extra.alarm.SKIP_UI': true,
'android.intent.extra.alarm.MESSAGE': 'Create a Flutter app',
},
);
intent.launch();
}
enum
constant creates an Enum
instance that has a toString()
method that will return a string with the name of the enum
type and the name of the constant. For example, given:enum MyEnum {
myConstant,
}
MyEnum.myConstant.toString()
will return "MyEnum.myConstant"
. Although this won't let you easily create arbitrary strings, you could do something like:enum alarm {
DAYS,
HOUR,
MINUTES,
SKIP_UI,
MESSAGE,
}
void _createAlarm() {
final arguments = <alarm, dynamic>{
alarm.DAYS: <int>[2, 3, 4, 5, 6],
alarm.HOUR: 21,
alarm.MINUTES: 30,
alarm.SKIP_UI: true,
alarm.MESSAGE: 'Create a Flutter app',
};
final AndroidIntent intent = const AndroidIntent(
action: 'android.intent.action.SET_ALARM',
arguments: arguments.map((k, v) => MapEntry('android.intent.extra.$k', v)),
);
intent.launch();
}