简体   繁体   English

包含对象列表的 dart 类构造函数

[英]dart class constructor to include list of objects

dart class飞镖课

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

Not clear if "List" is declared correctly there.不清楚“列表”是否在那里正确声明。 If yes, how to parse from json data using factory?如果是,如何使用工厂从 json 数据中解析?

here is a JSON data:这是一个 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"}]}]}

Here is an example of how you could do it:这是一个如何做到的示例:

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)])
}

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

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