在打印“设置”对象时,控制台上不会打印任何内容

时间:2019-12-04 11:16:55

标签: flutter set

我正在对Set类的对象执行某些操作后尝试打印结果。

到目前为止我尝试过的代码...

void main() {

 final t1 = TwoWheeler("Honda", "CB Shine", 4);
 final t2 = TwoWheeler("Honda", "Twister", 4);
 final t3 = TwoWheeler("Hero", "Splendar", 4);
 final t4 = TwoWheeler("Bajaj", "Pulser", 5);

 List list = [t1, t2, t3, t4];

 var result = list.map( (ss) {
  return ss.gear < 5 ? ss.desc() : null;
 });
 print("2-wheeler with 4 gears are $result"); //The result is get printed as I want

 var set = new Set<String>();
 set = result.toSet();
 print("2-wheeler with 4 gears are $set"); //Nothing printed on console

 set.removeWhere( (ss) => ss != null);
 print("2-wheeler with 4 gears are $set"); //Nothing printed on console

}

输出

2-wheeler with 4 gears are (Honda CB Shine with 4 gear(s), Honda Twister with 4 gear(s), Hero Splendar with 4 gear(s), null)

//Other 2 print statement is not displaying anything.

我想念什么吗?甚至编译器也没有抱怨这个问题。

我正在使用Dart Pad

1 个答案:

答案 0 :(得分:1)

尝试这样,让我知道它是否有效!

 var result = list.where( (ss) {
             return ss.gear < 5;
            });
 print("2-wheeler with 4 gears are $result");

 var set = Set.from(result);
 print("2-wheeler with 4 gears are $set"); //Nothing printed on console

 set.removeWhere( (ss) => ss != '');
 print("2-wheeler with 4 gears are $set");