json - 与颜色相关的 flutter 图像

标签 json laravel api button flutter

我正在制作一个 API 来显示产品图片和颜色, 每当用户按下颜色按钮时,与该颜色关联的图像都应使用 PageView.builder 显示。 如果这是我的 json,我如何为 api 创建模型工厂?

[
    {
        "id": 7,
        "name": "1",
        "descriptions": "1",
        "price": 1,
        "discount": 1,
        "category": "new",
        "quantity": 1,
        "brand": "1",
        "image": "",
        "created_at": "2019-08-04 09:07:25",
        "updated_at": "2019-08-04 09:07:25",
        "images": {
            "url":"1564909645iKiw2LkoEcQIIhB4MTZJTUfwTREleWH4wEuvmRPd.png",
            "color":"#fffddd"
        },
        "tags": [
            "large"
        ],
        "sizes": []
    }
]

我的模型和工厂:

class Response1 {
  final String createdAt;
  final int id;
  final int quantity;
  final double price;
  final String name;
  final String descriptions;
  final String updatedAt;
  final String image;
  final int weight;
  final List images;
  final List tags;
  final List sizes;
  Response1(
      {this.createdAt,
        this.id,
        this.quantity,
        this.price,
        this.name,
        this.descriptions,
        this.updatedAt,
        this.image,
        this.weight,
        this.images,
        this.tags,
        this.sizes});

  factory Response1.fromJson(Map<String, dynamic> json) {
    return Response1(
      createdAt: json['created_at'] as String,
      id: json['id'] as int,
      quantity: json['quantity'] as int,
      price: _toDouble(json['price']),
      name: json['name'] as String,
      updatedAt: json['updated_at'] as String,
      image: json['image'] as String,
      descriptions: json['descriptions'] as String,
      weight: json['weight'] as int,
      images: json['images'] as List,
      tags: json['tags'] as List,
      sizes: json['sizes'] as List,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'created_at': createdAt,
      'id': id,
      'quantity': quantity,
      'price': price,
      'name': name,
      'updated_at': updatedAt,
      'image': image,
      'weight': weight,
      'images': images,
      'tags': tags,
      'sizes': sizes,
    };
  }
}

我该怎么做?还有别的办法吗? 谢谢

最佳答案

你可以使用https://app.quicktype.io/帮助您简化此操作
你需要的代码请看下面

// To parse this JSON data, do
//
//     final response1 = response1FromJson(jsonString);

import 'dart:convert';

List<Response1> response1FromJson(String str) => new List<Response1>.from(json.decode(str).map((x) => Response1.fromJson(x)));

String response1ToJson(List<Response1> data) => json.encode(new List<dynamic>.from(data.map((x) => x.toJson())));

class Response1 {
    int id;
    String name;
    String descriptions;
    int price;
    int discount;
    String category;
    int quantity;
    String brand;
    String image;
    DateTime createdAt;
    DateTime updatedAt;
    Images images;
    List<String> tags;
    List<dynamic> sizes;

    Response1({
        this.id,
        this.name,
        this.descriptions,
        this.price,
        this.discount,
        this.category,
        this.quantity,
        this.brand,
        this.image,
        this.createdAt,
        this.updatedAt,
        this.images,
        this.tags,
        this.sizes,
    });

    factory Response1.fromJson(Map<String, dynamic> json) => new Response1(
        id: json["id"],
        name: json["name"],
        descriptions: json["descriptions"],
        price: json["price"],
        discount: json["discount"],
        category: json["category"],
        quantity: json["quantity"],
        brand: json["brand"],
        image: json["image"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
        images: Images.fromJson(json["images"]),
        tags: new List<String>.from(json["tags"].map((x) => x)),
        sizes: new List<dynamic>.from(json["sizes"].map((x) => x)),
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "descriptions": descriptions,
        "price": price,
        "discount": discount,
        "category": category,
        "quantity": quantity,
        "brand": brand,
        "image": image,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
        "images": images.toJson(),
        "tags": new List<dynamic>.from(tags.map((x) => x)),
        "sizes": new List<dynamic>.from(sizes.map((x) => x)),
    };
}

class Images {
    String url;
    String color;

    Images({
        this.url,
        this.color,
    });

    factory Images.fromJson(Map<String, dynamic> json) => new Images(
        url: json["url"],
        color: json["color"],
    );

    Map<String, dynamic> toJson() => {
        "url": url,
        "color": color,
    };
}

关于json - 与颜色相关的 flutter 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57408064/

相关文章:

php - Laravel - 在子文件夹中返回 View

python - 500 内部服务器错误 : Python Bottle API

c# - 无法将字符串作为 HTTPContent 发送

python - 如何从API中抓取NHL球队的名称和赔率?

php - 如何在 laravel 范围内查询关系

json - 带有选项字段的 F# 记录在 Asp.Net WebApi 2.x 应用程序中未正确反序列化

laravel - 为集合分页,Laravel

azure - 如何使用 Python 在 Azure Devops 中创建 Wiki 子页面?

java - 如何从 HashMap<String,Object> 中的 gson 获取字符串,如下所示

php - 如何解码 google 搜索 api 结果?