Flutter 对于ex Type1<;如何获取dart中泛型对象的主类型和子类型;类型2>;,我只想把1号和2号分开
我想要颤振中泛型类型的主类型和子类型。 例如,如果我有Flutter 对于ex Type1<;如何获取dart中泛型对象的主类型和子类型;类型2>;,我只想把1号和2号分开,flutter,dart,Flutter,Dart,我想要颤振中泛型类型的主类型和子类型。 例如,如果我有List,我希望List和int都是分开的。如果我执行[1,2,3].runtimeType,它会给我列表。但若我只对列表或该信息的int部分感兴趣,那个该怎么办呢 我正在创建一个类型比较器,用于对数据进行排序 int typeSortHelper(Type type1, Type type2){ if(type1 == type2){ return 0; } if(type2 is List){ type2 =
List
,我希望List
和int
都是分开的。如果我执行[1,2,3].runtimeType
,它会给我列表
。但若我只对列表或该信息的int部分感兴趣,那个该怎么办呢
我正在创建一个类型比较器,用于对数据进行排序
int typeSortHelper(Type type1, Type type2){
if(type1 == type2){
return 0;
}
if(type2 is List){
type2 = List;
}
switch(type1){
case int: return 1;
case double:{
int r;
if(type2 == int){
r= -1;
}else{
r= 1;
}
return r;
}
case String:{
int r;
if(type2 == int || type2 == double){
r= -1;
}else{
r= 1;
}
return r;
}
case List:{
int r;
if(type2 == int || type2 == double || type2 == String){
r= -1;
}else{
r= 1;
}
return r;
}
case Map:{
int r;
if(type2 == int || type2 == double || type2 == String || type2 == List){
r= -1;
}else{
r= 1;
}
return r;
}
}
}
所以当我打这样的电话时:
r1 = typeSortHelper([1,2,3].runtimeType, 2.runtimeType);
它应该给我-1,而不是空值。遗憾的是,你还不能这样做 因此,2019年(Dart 2.5)的实施情况如下:
switch(smth.runtimeType){
case int: return processInt(smth);
case Uint8List: return process Uint8List(smth):
default: if(smth is Map<String, dynamic>){
processMapStringDynamic(smth);
} else if(smth is List<int>){
processListInt(smth);
} else throw('unimplemented for ${smth.runtimeType}');
}
开关(smth.runtimeType){
案例int:返回进程int(smth);
案例UINT8列表:返回流程UINT8列表(smth):
默认值:如果(smth是映射){
processMapStringDynamic(smth);
}否则如果(smth为列表){
processListInt(smth);
}else throw(${smth.runtimeType}未实现);
}