Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Dart中,有比大型switch语句更好的解决方案吗_Dart - Fatal编程技术网

在Dart中,有比大型switch语句更好的解决方案吗

在Dart中,有比大型switch语句更好的解决方案吗,dart,Dart,我有一个大型多级switch语句,用于获取表/字段ID组合并返回字符串。dart中是否有一个系统在读取能力和性能方面比switch语句更好?我应该使用地图之类的东西吗 这是switch语句的一个片段,完成后将有100行 switch (tableID) { case DBTables.Abbreviations: switch (fieldID) { case TBAbbreviations.

我有一个大型多级switch语句,用于获取表/字段ID组合并返回字符串。dart中是否有一个系统在读取能力和性能方面比switch语句更好?我应该使用地图之类的东西吗

这是switch语句的一个片段,完成后将有100行

switch (tableID)
    {
        case DBTables.Abbreviations:
            switch (fieldID)
            {
                case TBAbbreviations.ID: result = 'Record ID'; break;
            }
            break;
        case DBTables.Activity:
            switch (fieldID)
            {
                case TBActivity.ID: result = 'Record ID'; break;
                case TBActivity.Nickname1: result = 'Nickname'; break;
                case TBActivity.Nickname2: result = 'Nickname 2'; break;                    
                case TBActivity.FullName: result = 'Fullname'; break;                    
                case TBActivity.Classification: result = 'Classification'; break;                    
            }
            break;
    }

    return 'Field Name: ' + result ;    

您没有编写tableID和fieldID的类型,但我猜它们都是
int

我制作了以下示例,该示例尝试划分映射的责任,以便每个表类都进行自己的映射:

class DBTables {
  static const int Abbreviations = 1;
  static const int Activity = 2;

  static String lookupFieldName(int tableID, int fieldID) {
    String result;

    if (tableID == Abbreviations) {
      result = TBAbbreviations.idToString[fieldID];
    } else if (tableID == Activity) {
      result = TBActivity.idToString[fieldID];
    }

    return 'Field Name: $result';
  }
}

class TBAbbreviations {
  static const int ID = 1;

  static const Map<int, String> idToString = {ID: 'Record ID'};
}

class TBActivity {
  static const int ID = 1;
  static const int Nickname1 = 2;
  static const int Nickname2 = 3;
  static const int FullName = 4;
  static const int Classification = 5;

  static const Map<int, String> idToString = {
    ID: 'Record ID',
    Nickname1: 'Nickname',
    Nickname2: 'Nickname 2',
    FullName: 'Fullname',
    Classification: 'Classification'
  };
}

void main() {
  print(DBTables.lookupFieldName(DBTables.Activity, TBActivity.FullName)); // Field Name: Fullname
}
classdbtables{
静态常量int缩写=1;
静态常数int活动=2;
静态字符串lookupFieldName(int tableID,int fieldID){
字符串结果;
if(tableID==缩写){
结果=tb缩写.idToString[fieldID];
}else if(tableID==活动){
结果=TBActivity.idToString[fieldID];
}
返回“字段名:$result”;
}
}
类别缩略语{
静态常量int ID=1;
静态常量映射idToString={ID:'记录ID'};
}
班级活动{
静态常量int ID=1;
静态常数1=2;
静态常数2=3;
静态常量int FullName=4;
静态常数int分类=5;
静态常量映射idToString={
ID:'记录ID',
昵称1:“昵称”,
昵称2:‘昵称2’,
全名:“全名”,
分类:“分类”
};
}
void main(){
打印(DBTables.lookupFieldName(DBTables.Activity,TBActivity.FullName));//字段名:FullName
}

您可以讨论
DBTables.lookupFieldName
的位置,但我认为这对于我的小示例来说是有意义的。

正如您所建议的,我将制作一个映射并使用它来查找结果。TableID和FieldID都是int(很抱歉)。我想这对我来说会很好。非常感谢。