【问题标题】:dart class constructor to include list of objectsdart 类构造函数以包含对象列表
【发布时间】:2021-10-30 01:05:33
【问题描述】:

飞镖类

    class Car {
       String make;
       String model;
    }
    class CelebrityProfile {
       String Name;
       String YOB;
       List<Car> CarList;
    }

不清楚“列表”是否在此处正确声明。如果是,如何使用工厂从 json 数据中解析?

这是一个 JSON 数据:

    {"CelebrityProfile":[{"Name":"Celeb_1","YOB":"2000","CarList":[{"make":"Lamborghini","model":"Aventador SVJ"},{"make":"Ferrari","model":"F8 Spider"}]},{"Name":"Celeb_2","YOB":"1995","CarList":[{"make":"Tesla","model":"S"},{"make":"Porsche","model":"911"},{"make":"Lamborghini","model":"Huracan EVO"}]}]}

【问题讨论】:

  • 你的 JSON 数据结构如何?你能举个例子吗?
  • 我认为你必须在发布之前更好地理解你的问题
  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: class dart constructor


【解决方案1】:

下面是一个例子:

import 'dart:convert';

class Car {
  final String make;
  final String model;

  Car({
    required this.make,
    required this.model,
  });

  factory Car.fromMap(Map<String, dynamic> map) => Car(
        make: map['make'] as String,
        model: map['model'] as String,
      );

  @override
  String toString() => 'Car(make: $make, model: $model)';
}

class CelebrityProfile {
  final String name;
  final String yob;
  final List<Car> carList;

  CelebrityProfile({
    required this.name,
    required this.yob,
    required this.carList,
  });

  factory CelebrityProfile.fromMap(Map<String, dynamic> map) =>
      CelebrityProfile(
        name: map['Name'] as String,
        yob: map['YOB'] as String,
        carList: [
          for (final subMap in map['CarList'])
            Car.fromMap(subMap as Map<String, dynamic>)
        ],
      );

  @override
  String toString() =>
      'CelebrityProfile(name: $name, yob: $yob, carList: $carList)';
}

void main() {
  const jsonInput = '''{
  "CelebrityProfile": [
    {
      "Name": "Celeb_1",
      "YOB": "2000",
      "CarList": [
        {
          "make": "Lamborghini",
          "model": "Aventador SVJ"
        },
        {
          "make": "Ferrari",
          "model": "F8 Spider"
        }
      ]
    },
    {
      "Name": "Celeb_2",
      "YOB": "1995",
      "CarList": [
        {
          "make": "Tesla",
          "model": "S"
        },
        {
          "make": "Porsche",
          "model": "911"
        },
        {
          "make": "Lamborghini",
          "model": "Huracan EVO"
        }
      ]
    }
  ]
}
  ''';

  final parsedJson = jsonDecode(jsonInput) as Map<String, dynamic>;
  final celebrityProfiles = [
    for (final celebrityProfileMap in parsedJson['CelebrityProfile'])
      CelebrityProfile.fromMap(celebrityProfileMap as Map<String, dynamic>)
  ];

  celebrityProfiles.forEach(print);
  // CelebrityProfile(name: Celeb_1, yob: 2000, carList: [Car(make: Lamborghini, model: Aventador SVJ), Car(make: Ferrari, model: F8 Spider)])
  // CelebrityProfile(name: Celeb_2, yob: 1995, carList: [Car(make: Tesla, model: S), Car(make: Porsche, model: 911), Car(make: Lamborghini, model: Huracan EVO)])
}

【讨论】:

  • 完美。学习了如何从 json 中提取 carList。谢谢
猜你喜欢
  • 1970-01-01
  • 2022-08-11
  • 1970-01-01
  • 2014-06-30
  • 1970-01-01
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多