. 如果数据在它的daysModel中作为DoctorModel,则在daysModel中是workTimeModel,flutter,dart,mobile,dart-pub"/>

首页 > 解决方案 > 当API的数据作为地图时如何从API获取数据. 如果数据在它的daysModel中作为DoctorModel,则在daysModel中是workTimeModel

问题描述

我想从 API 中获取数据,我的 API 数据在它的 daysModel 里面作为 DoctorModel,在 daysModel 里面是 workTimeModel,每个医生都有很多天并且有工作时间。

我尝试了很多方法,但仍然无法解决它。

注意:我从这个网站https://app.quicktype.io/制作了我的 API

我获取 API 数据的代码:

Response res = await get(
      doctorsUrl ,
    );
    if (res.statusCode == 200) {
      var body = jsonDecode(res.body);
      List<dynamic> data = body['data'];
      List<DoctorInfoModel> doctors =  data.map((dynamic item) => DoctorInfoModel.fromJson(item)).toList();
      return doctors;
    }

我的 API:

{
        "id": 15,
        "name": "Prof. Elton Quigley",
        "about": "uHiKeKA1gq",
        "stars": 5,
        "location": "R59lmj1eud",
        "latitude": 5,
        "longitude": 5,
        "notes": "yCl95VqUAz",
        "days": [
            {
                "name": "سبت",
                "pivot": {
                    "doctor_id": 15,
                    "day_id": 1,
                    "morning": "1",
                    "evening": "1"
                }
            },
            {
                "name": "أحد",
                "pivot": {
                    "doctor_id": 15,
                    "day_id": 2,
                    "morning": "3",
                    "evening": "3"
                }
            },
            {
                "name": "إثنين",
                "pivot": {
                    "doctor_id": 15,
                    "day_id": 3,
                    "morning": "5",
                    "evening": "5"
                }
            },
            {
                "name": "ثلاثاء",
                "pivot": {
                    "doctor_id": 15,
                    "day_id": 4,
                    "morning": "4",
                    "evening": "4"
                }
            },
            {
                "name": "أربعاء",
                "pivot": {
                    "doctor_id": 15,
                    "day_id": 5,
                    "morning": "5",
                    "evening": "5"
                }
            },
            {
                "name": "خميس",
                "pivot": {
                    "doctor_id": 15,
                    "day_id": 6,
                    "morning": "4",
                    "evening": "4"
                }
            }
        ]
    }

我的医生模型:

// To parse this JSON data, do
//
//     final doctorInfoModel = doctorInfoModelFromJson(jsonString);

import 'dart:convert';

DoctorInfoModel doctorInfoModelFromJson(String str) => DoctorInfoModel.fromJson(json.decode(str));

String doctorInfoModelToJson(DoctorInfoModel data) => json.encode(data.toJson());

class DoctorInfoModel {
    DoctorInfoModel({
        this.id,
        this.name,
        this.about,
        this.stars,
        this.location,
        this.latitude,
        this.longitude,
        this.notes,
        this.days,
    });

    int id;
    String name;
    String about;
    int stars;
    String location;
    int latitude;
    int longitude;
    String notes;
    List<Day> days;

    factory DoctorInfoModel.fromJson(Map<String, dynamic> json) => DoctorInfoModel(
        id: json["id"],
        name: json["name"],
        about: json["about"],
        stars: json["stars"],
        location: json["location"],
        latitude: json["latitude"],
        longitude: json["longitude"],
        notes: json["notes"],
        days: List<Day>.from(json["days"].map((x) => Day.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "about": about,
        "stars": stars,
        "location": location,
        "latitude": latitude,
        "longitude": longitude,
        "notes": notes,
        "days": List<dynamic>.from(days.map((x) => x.toJson())),
    };
}

class Day {
    Day({
        this.name,
        this.pivot,
    });

    String name;
    Pivot pivot;

    factory Day.fromJson(Map<String, dynamic> json) => Day(
        name: json["name"],
        pivot: Pivot.fromJson(json["pivot"]),
    );

    Map<String, dynamic> toJson() => {
        "name": name,
        "pivot": pivot.toJson(),
    };
}

class Pivot {
    Pivot({
        this.doctorId,
        this.dayId,
        this.morning,
        this.evening,
    });

    int doctorId;
    int dayId;
    String morning;
    String evening;

    factory Pivot.fromJson(Map<String, dynamic> json) => Pivot(
        doctorId: json["doctor_id"],
        dayId: json["day_id"],
        morning: json["morning"],
        evening: json["evening"],
    );

    Map<String, dynamic> toJson() => {
        "doctor_id": doctorId,
        "day_id": dayId,
        "morning": morning,
        "evening": evening,
    };
}

如何正确获取数据?

标签: flutterdartmobiledart-pub

解决方案


我想出了如何获取数据。

我替换了这两行:

List<dynamic> data = body['data'];
List<DoctorInfoModel> doctors =  data.map((dynamic item) => DoctorInfoModel.fromJson(item)).toList();

通过这些:

var data = body['data'];
DoctorInfoModel doctorInfo = DoctorInfoModel.fromJson(data);

当我DoctorInfoModel不是 a时List,我将其删除。

获取 API 数据的正确代码:

 if (res.statusCode == 200) {
      var body = jsonDecode(res.body);
      var data = body['data'];
      DoctorInfoModel doctorInfo = DoctorInfoModel.fromJson(data);
      print(doctorInfo.name);
      print(doctorInfo.about);
      print(doctorInfo.days[0].name);
      print(doctorInfo.days[0].pivot.morning);
      return doctorInfo;
    }

推荐阅读