飞镖替代很多if elseiif [英] dart alternate for a lot of if elseiif

查看:68
本文介绍了飞镖替代很多if elseiif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to replace an URL String in Dart with another String. Example:

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

有没有一种更少的代码,也许更快的更好的方法?我必须做60多次..

Isn't there a better way with less code and maybe faster? I'd have to do this over 60 times..

推荐答案

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

If you want less code, you can do somtehing like this :

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 elseiif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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