Dart中的字符串文字类型(如TypeScript中的字符串)? [英] String Literal Types (like in TypeScript) in Dart?

查看:135
本文介绍了Dart中的字符串文字类型(如TypeScript中的字符串)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Flutter的帮助,我开始使用Dart。



我以前使用TypeScript,它提供了一些以前从未见过的非常酷的功能。 ,我特别喜欢的是字符串文字类型,看起来像此

  type缓和=缓入 | 缓解 | 由内而外; 
函数doSomething(easing:Easing){/ *做某事* /}
doSomething( ease-in); // OK
doSomething( easy); //错误

在Dart中,我发现使用枚举静态字符串 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();
}

Dart中是否有一种方法可以使这些魔术字符串像在TypeScript的字符串文字类型中一样?

解决方案

每个枚举常量会创建 Enum toString()方法的c $ c> 实例,该方法将返回名称为 enum 类型和常量的名称。例如,给定:

 枚举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();
}


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

type Easing = "ease-in" | "ease-out" | "ease-in-out";
function doSomething(easing: Easing) { /* do something */}
doSomething("ease-in");  // OK
doSomething("easy");  // ERROR

In Dart, I find using enums 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 Strings or enums.

One example of this would be from the 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();
}

Is there a way in Dart to have these "magic strings" like in TypeScript's string literal types?

解决方案

Each 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,
}

then 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();
}

这篇关于Dart中的字符串文字类型(如TypeScript中的字符串)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆