if-statement - dart 替代很多 if else if

标签 if-statement flutter dart

我想用另一个字符串替换 Dart 中的 URL 字符串。 示例:

if (url == "http://www.example.com/1") {
home = "example";
} else if (url == "http://www.example.com/2") {
home = "another example";
}

有没有更好的方法,代码更少而且速度更快?我必须这样做 60 多次..

最佳答案

如果你想要更少的代码,你可以这样做:

homes = {
  "http://www.example.com/1": "example",
  "http://www.example.com/2": "another example",
  "http://www.example.com/3": "yet another one",
};
home = homes[url];

关于if-statement - dart 替代很多 if else if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57249379/

相关文章:

dart - pubspec.yaml不推荐使用的布局

java - 扫描仪在 if 语句内无法正常工作

javascript - 如果语句不能正常工作

python - 嵌套 if 循环在 python 中未触发

flutter - 使用 flutter firebase_messaging 插件发送通知声音

java - 无法解析符号 "tensorflow"flutter 框架

flutter : How can i create this circle avatar that's half out of the container?

javascript - 是否有 Javascript 到 Dart 的转换器?

if-statement - 如果存在 C :\directory\goto a else goto b problems windows XP batch files

flutter - flutter ,显示问题实例