【问题标题】:how flutter map in for widget小部件的颤动映射如何
【发布时间】:2020-04-03 16:02:56
【问题描述】:

我的 json=

{
  "result": {
    "name": "json1",
      "pages": [{
          "zones": [{
              "title": "title1"
           },
           {
              "title": "title2"
           }],
           "id": 4
       },
       {
          "zones": [{
            "title": "title3"
          },
          {
            "title": "title4"
          }],
          "id": 12
       }],
       "creatorUserName": "admin",
       "id": 2
    }
}

futurebuilder 代码

List post = snapshot.data["result"]["pages"];

return new Stack(
              children: post.where((val) => val["id"] == 4).map((post) {
                for (var item in post['zones']) {
                print("title "+ item['title']);
                  Container(
                    child: Text(item["title"]),
                  ); //Container
                }


              }).toList(),

); //Stack

错误代码:堆栈的子级不得包含任何空值,但在索引 0 处找到空值 enter image description here

帮助如何构建算法

如果得到 id = 4 个区域 -> Text(title1), Text(title2),


else id empty zone -> Text(title1), Text(title2), zone -> Text(title3), Text(title4),

【问题讨论】:

    标签: flutter dart flutter-web


    【解决方案1】:

    试试

    List post = snapshots.data["result"]["pages"];
    

    【讨论】:

      【解决方案2】:

      首先使用这个惊人的webpage 为您的 JSON 响应创建一个模型类,然后您就可以轻松地进行操作了。调用需要的数据

      import 'dart:convert';
      
      YourModelClassName yourModelClassNameFromJson(String str) => YourModelClassName.fromJson(json.decode(str));
      
      String yourModelClassNameToJson(YourModelClassName data) => json.encode(data.toJson());
      
      class YourModelClassName {
          Result result;
      
          YourModelClassName({
              this.result,
          });
      
          factory YourModelClassName.fromJson(Map<String, dynamic> json) => YourModelClassName(
              result: Result.fromJson(json["result"]),
          );
      
          Map<String, dynamic> toJson() => {
              "result": result.toJson(),
          };
      }
      
      class Result {
          String name;
          List<Page> pages;
          String creatorUserName;
          int id;
      
          Result({
              this.name,
              this.pages,
              this.creatorUserName,
              this.id,
          });
      
          factory Result.fromJson(Map<String, dynamic> json) => Result(
              name: json["name"],
              pages: List<Page>.from(json["pages"].map((x) => Page.fromJson(x))),
              creatorUserName: json["creatorUserName"],
              id: json["id"],
          );
      
          Map<String, dynamic> toJson() => {
              "name": name,
              "pages": List<dynamic>.from(pages.map((x) => x.toJson())),
              "creatorUserName": creatorUserName,
              "id": id,
          };
      }
      
      class Page {
          List<Zone> zones;
          int id;
      
          Page({
              this.zones,
              this.id,
          });
      
          factory Page.fromJson(Map<String, dynamic> json) => Page(
              zones: List<Zone>.from(json["zones"].map((x) => Zone.fromJson(x))),
              id: json["id"],
          );
      
          Map<String, dynamic> toJson() => {
              "zones": List<dynamic>.from(zones.map((x) => x.toJson())),
              "id": id,
          };
      }
      
      class Zone {
          String title;
      
          Zone({
              this.title,
          });
      
          factory Zone.fromJson(Map<String, dynamic> json) => Zone(
              title: json["title"],
          );
      
          Map<String, dynamic> toJson() => {
              "title": title,
          };
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-04
        • 2021-08-27
        • 2019-05-23
        • 2021-02-01
        • 1970-01-01
        • 2021-09-05
        • 2020-09-27
        • 2022-01-06
        • 2021-10-11
        相关资源
        最近更新 更多