繁体   English   中英

“Null”类型不是“String”类型的子类型 flutter / dart

[英]type 'Null' is not a subtype of type 'String' flutter / dart

我有这个 json,我想显示variation的键和值(我应该提取键和值,因为它们不稳定)。

{
  "data" : [
     "items": {
                "388488": {
                    "id": 388488,
                    "name": "Galaxy Tab A7 10.4 2020",
                    "qty": 1,
                    "variations": {
                        "color": "green",
                                   },
                           },
                "388489": {
                    "id": 388489,
                    "name": "Samsung Galaxy Tab A7 10.4 2020",
                    "qty": 1,
                    "variations": {
                        "color": "red",
                        "something": "somth"
                                   },
                },
                "388490": {
                    "id": 388490,
                    "name": " Apple iPad 10.2",
                    "qty": 1,
                    "variations": {
                        "color": "blue",
                        "something1": "somth1"
                                   },
                }
            }
       ]
 }

当我以这种方式打印变体时,它很好并打印所有变体:

  itemDetail.variations.forEach((key, value) {
       print(value);
     });

但是当我尝试在文本小部件中显示它时,它说变化是null

                  Text(
                            itemDetail.variations.forEach((key , value){
                              String Description = "$key : $value";
                              print(Description); // it just print the variation of first item
                              return Description; // I also tried without return, same error
                            }),
                            textDirection: TextDirection.rtl,),

项目详细信息是项目 model class 的 object 此处为:

class Items{
  late final id;
  late final name;
  late final quantity;
  late final variations;
  Items({
   this.id,
   this.name,
   this.quantity,
    this.variations
});
  
  
  factory Items.fromJson(Map<String , dynamic> Json){
    return Items(
      id: Json["id"],
      name: Json["name"],
      quantity: Json["qty"],
        variations: Json["variations"] ?? ""
    );
  }
}

而不是 forEach 尝试 map 因为 forEach 不返回任何东西。

 Text(
       itemDetail.variations.map((key , value){
       String Description = "$key : $value";
       print(Description); // it just print the variation of first item
      return Description; // I also tried without return, same error
      }),
     textDirection: TextDirection.rtl,),

您可能希望您的列表视图类似于

ListView(
        children: itemDetail.variations.entries
            .map((entry) => Text("${entry.key} : ${entry.value}"))
            .toList());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM