我想用另一个字符串替换 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/