簡體   English   中英

flutter map 如何用於小部件

[英]how flutter map in for widget

我的 json =

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

未來建造者代碼

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

錯誤代碼:堆棧的子項不得包含任何 null 值,但在索引 0 處找到 null 值 在此處輸入圖像描述

幫助如何構建算法

如果得到 id = 4 個區域 -> 文本(標題 1),文本(標題 2),


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

嘗試

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

首先使用這個令人驚嘆的網頁為您的 JSON 響應創建 model class ,之后您可以輕松。 調用需要的數據

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,
    };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM