这是一个完整的模型类,您可以阅读和理解。花了太多时间,但我很感激你的问题。
您只需复制并将其保存在单独的文件中,例如 model.dart:
// To parse this JSON data, do
//
// final welcome = welcomeFromMap(jsonString);
import 'dart:convert';
class Welcome {
Welcome({
this.code,
this.status,
this.data,
});
final int code;
final String status;
final List<Datum> data;
factory Welcome.fromJson(String str) => Welcome.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Welcome.fromMap(Map<String, dynamic> json) => Welcome(
code: json["code"] == null ? null : json["code"],
status: json["status"] == null ? null : json["status"],
data: json["data"] == null ? null : List<Datum>.from(json["data"].map((x) => Datum.fromMap(x))),
);
Map<String, dynamic> toMap() => {
"code": code == null ? null : code,
"status": status == null ? null : status,
"data": data == null ? null : List<dynamic>.from(data.map((x) => x.toMap())),
};
}
class Datum {
Datum({
this.timings,
this.date,
this.meta,
});
final Timings timings;
final Date date;
final Meta meta;
factory Datum.fromJson(String str) => Datum.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Datum.fromMap(Map<String, dynamic> json) => Datum(
timings: json["timings"] == null ? null : Timings.fromMap(json["timings"]),
date: json["date"] == null ? null : Date.fromMap(json["date"]),
meta: json["meta"] == null ? null : Meta.fromMap(json["meta"]),
);
Map<String, dynamic> toMap() => {
"timings": timings == null ? null : timings.toMap(),
"date": date == null ? null : date.toMap(),
"meta": meta == null ? null : meta.toMap(),
};
}
class Date {
Date({
this.readable,
this.timestamp,
this.gregorian,
this.hijri,
});
final String readable;
final String timestamp;
final Gregorian gregorian;
final Hijri hijri;
factory Date.fromJson(String str) => Date.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Date.fromMap(Map<String, dynamic> json) => Date(
readable: json["readable"] == null ? null : json["readable"],
timestamp: json["timestamp"] == null ? null : json["timestamp"],
gregorian: json["gregorian"] == null ? null : Gregorian.fromMap(json["gregorian"]),
hijri: json["hijri"] == null ? null : Hijri.fromMap(json["hijri"]),
);
Map<String, dynamic> toMap() => {
"readable": readable == null ? null : readable,
"timestamp": timestamp == null ? null : timestamp,
"gregorian": gregorian == null ? null : gregorian.toMap(),
"hijri": hijri == null ? null : hijri.toMap(),
};
}
class Gregorian {
Gregorian({
this.date,
this.format,
this.day,
this.weekday,
this.month,
this.year,
this.designation,
});
final String date;
final Format format;
final String day;
final GregorianWeekday weekday;
final GregorianMonth month;
final String year;
final Designation designation;
factory Gregorian.fromJson(String str) => Gregorian.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Gregorian.fromMap(Map<String, dynamic> json) => Gregorian(
date: json["date"] == null ? null : json["date"],
format: json["format"] == null ? null : formatValues.map[json["format"]],
day: json["day"] == null ? null : json["day"],
weekday: json["weekday"] == null ? null : GregorianWeekday.fromMap(json["weekday"]),
month: json["month"] == null ? null : GregorianMonth.fromMap(json["month"]),
year: json["year"] == null ? null : json["year"],
designation: json["designation"] == null ? null : Designation.fromMap(json["designation"]),
);
Map<String, dynamic> toMap() => {
"date": date == null ? null : date,
"format": format == null ? null : formatValues.reverse[format],
"day": day == null ? null : day,
"weekday": weekday == null ? null : weekday.toMap(),
"month": month == null ? null : month.toMap(),
"year": year == null ? null : year,
"designation": designation == null ? null : designation.toMap(),
};
}
class Designation {
Designation({
this.abbreviated,
this.expanded,
});
final Abbreviated abbreviated;
final Expanded expanded;
factory Designation.fromJson(String str) => Designation.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Designation.fromMap(Map<String, dynamic> json) => Designation(
abbreviated: json["abbreviated"] == null ? null : abbreviatedValues.map[json["abbreviated"]],
expanded: json["expanded"] == null ? null : expandedValues.map[json["expanded"]],
);
Map<String, dynamic> toMap() => {
"abbreviated": abbreviated == null ? null : abbreviatedValues.reverse[abbreviated],
"expanded": expanded == null ? null : expandedValues.reverse[expanded],
};
}
enum Abbreviated { AD, AH }
final abbreviatedValues = EnumValues({
"AD": Abbreviated.AD,
"AH": Abbreviated.AH
});
enum Expanded { ANNO_DOMINI, ANNO_HEGIRAE }
final expandedValues = EnumValues({
"Anno Domini": Expanded.ANNO_DOMINI,
"Anno Hegirae": Expanded.ANNO_HEGIRAE
});
enum Format { DD_MM_YYYY }
final formatValues = EnumValues({
"DD-MM-YYYY": Format.DD_MM_YYYY
});
class GregorianMonth {
GregorianMonth({
this.number,
this.en,
});
final int number;
final PurpleEn en;
factory GregorianMonth.fromJson(String str) => GregorianMonth.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory GregorianMonth.fromMap(Map<String, dynamic> json) => GregorianMonth(
number: json["number"] == null ? null : json["number"],
en: json["en"] == null ? null : purpleEnValues.map[json["en"]],
);
Map<String, dynamic> toMap() => {
"number": number == null ? null : number,
"en": en == null ? null : purpleEnValues.reverse[en],
};
}
enum PurpleEn { JUNE }
final purpleEnValues = EnumValues({
"June": PurpleEn.JUNE
});
class GregorianWeekday {
GregorianWeekday({
this.en,
});
final String en;
factory GregorianWeekday.fromJson(String str) => GregorianWeekday.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory GregorianWeekday.fromMap(Map<String, dynamic> json) => GregorianWeekday(
en: json["en"] == null ? null : json["en"],
);
Map<String, dynamic> toMap() => {
"en": en == null ? null : en,
};
}
class Hijri {
Hijri({
this.date,
this.format,
this.day,
this.weekday,
this.month,
this.year,
this.designation,
this.holidays,
});
final String date;
final Format format;
final String day;
final HijriWeekday weekday;
final HijriMonth month;
final String year;
final Designation designation;
final List<dynamic> holidays;
factory Hijri.fromJson(String str) => Hijri.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Hijri.fromMap(Map<String, dynamic> json) => Hijri(
date: json["date"] == null ? null : json["date"],
format: json["format"] == null ? null : formatValues.map[json["format"]],
day: json["day"] == null ? null : json["day"],
weekday: json["weekday"] == null ? null : HijriWeekday.fromMap(json["weekday"]),
month: json["month"] == null ? null : HijriMonth.fromMap(json["month"]),
year: json["year"] == null ? null : json["year"],
designation: json["designation"] == null ? null : Designation.fromMap(json["designation"]),
holidays: json["holidays"] == null ? null : List<dynamic>.from(json["holidays"].map((x) => x)),
);
Map<String, dynamic> toMap() => {
"date": date == null ? null : date,
"format": format == null ? null : formatValues.reverse[format],
"day": day == null ? null : day,
"weekday": weekday == null ? null : weekday.toMap(),
"month": month == null ? null : month.toMap(),
"year": year == null ? null : year,
"designation": designation == null ? null : designation.toMap(),
"holidays": holidays == null ? null : List<dynamic>.from(holidays.map((x) => x)),
};
}
class HijriMonth {
HijriMonth({
this.number,
this.en,
this.ar,
});
final int number;
final FluffyEn en;
final Ar ar;
factory HijriMonth.fromJson(String str) => HijriMonth.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory HijriMonth.fromMap(Map<String, dynamic> json) => HijriMonth(
number: json["number"] == null ? null : json["number"],
en: json["en"] == null ? null : fluffyEnValues.map[json["en"]],
ar: json["ar"] == null ? null : arValues.map[json["ar"]],
);
Map<String, dynamic> toMap() => {
"number": number == null ? null : number,
"en": en == null ? null : fluffyEnValues.reverse[en],
"ar": ar == null ? null : arValues.reverse[ar],
};
}
enum Ar { EMPTY, AR }
final arValues = EnumValues({
"ذوالقعدة": Ar.AR,
"شَوّال": Ar.EMPTY
});
enum FluffyEn { SHAWWL, DH_AL_QA_DAH }
final fluffyEnValues = EnumValues({
"Dhū al-Qaʿdah": FluffyEn.DH_AL_QA_DAH,
"Shawwāl": FluffyEn.SHAWWL
});
class HijriWeekday {
HijriWeekday({
this.en,
this.ar,
});
final String en;
final String ar;
factory HijriWeekday.fromJson(String str) => HijriWeekday.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory HijriWeekday.fromMap(Map<String, dynamic> json) => HijriWeekday(
en: json["en"] == null ? null : json["en"],
ar: json["ar"] == null ? null : json["ar"],
);
Map<String, dynamic> toMap() => {
"en": en == null ? null : en,
"ar": ar == null ? null : ar,
};
}
class Meta {
Meta({
this.latitude,
this.longitude,
this.timezone,
this.method,
this.latitudeAdjustmentMethod,
this.midnightMode,
this.school,
this.offset,
});
final double latitude;
final double longitude;
final Timezone timezone;
final Method method;
final LatitudeAdjustmentMethod latitudeAdjustmentMethod;
final MidnightMode midnightMode;
final MidnightMode school;
final Map<String, int> offset;
factory Meta.fromJson(String str) => Meta.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Meta.fromMap(Map<String, dynamic> json) => Meta(
latitude: json["latitude"] == null ? null : json["latitude"].toDouble(),
longitude: json["longitude"] == null ? null : json["longitude"].toDouble(),
timezone: json["timezone"] == null ? null : timezoneValues.map[json["timezone"]],
method: json["method"] == null ? null : Method.fromMap(json["method"]),
latitudeAdjustmentMethod: json["latitudeAdjustmentMethod"] == null ? null : latitudeAdjustmentMethodValues.map[json["latitudeAdjustmentMethod"]],
midnightMode: json["midnightMode"] == null ? null : midnightModeValues.map[json["midnightMode"]],
school: json["school"] == null ? null : midnightModeValues.map[json["school"]],
offset: json["offset"] == null ? null : Map.from(json["offset"]).map((k, v) => MapEntry<String, int>(k, v)),
);
Map<String, dynamic> toMap() => {
"latitude": latitude == null ? null : latitude,
"longitude": longitude == null ? null : longitude,
"timezone": timezone == null ? null : timezoneValues.reverse[timezone],
"method": method == null ? null : method.toMap(),
"latitudeAdjustmentMethod": latitudeAdjustmentMethod == null ? null : latitudeAdjustmentMethodValues.reverse[latitudeAdjustmentMethod],
"midnightMode": midnightMode == null ? null : midnightModeValues.reverse[midnightMode],
"school": school == null ? null : midnightModeValues.reverse[school],
"offset": offset == null ? null : Map.from(offset).map((k, v) => MapEntry<String, dynamic>(k, v)),
};
}
enum LatitudeAdjustmentMethod { ANGLE_BASED }
final latitudeAdjustmentMethodValues = EnumValues({
"ANGLE_BASED": LatitudeAdjustmentMethod.ANGLE_BASED
});
class Method {
Method({
this.id,
this.name,
this.params,
});
final int id;
final Name name;
final Params params;
factory Method.fromJson(String str) => Method.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Method.fromMap(Map<String, dynamic> json) => Method(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : nameValues.map[json["name"]],
params: json["params"] == null ? null : Params.fromMap(json["params"]),
);
Map<String, dynamic> toMap() => {
"id": id == null ? null : id,
"name": name == null ? null : nameValues.reverse[name],
"params": params == null ? null : params.toMap(),
};
}
enum Name { GULF_REGION }
final nameValues = EnumValues({
"Gulf Region": Name.GULF_REGION
});
class Params {
Params({
this.fajr,
this.isha,
});
final double fajr;
final Isha isha;
factory Params.fromJson(String str) => Params.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Params.fromMap(Map<String, dynamic> json) => Params(
fajr: json["Fajr"] == null ? null : json["Fajr"].toDouble(),
isha: json["Isha"] == null ? null : ishaValues.map[json["Isha"]],
);
Map<String, dynamic> toMap() => {
"Fajr": fajr == null ? null : fajr,
"Isha": isha == null ? null : ishaValues.reverse[isha],
};
}
enum Isha { THE_90_MIN }
final ishaValues = EnumValues({
"90 min": Isha.THE_90_MIN
});
enum MidnightMode { STANDARD }
final midnightModeValues = EnumValues({
"STANDARD": MidnightMode.STANDARD
});
enum Timezone { EUROPE_LONDON }
final timezoneValues = EnumValues({
"Europe/London": Timezone.EUROPE_LONDON
});
class Timings {
Timings({
this.fajr,
this.sunrise,
this.dhuhr,
this.asr,
this.sunset,
this.maghrib,
this.isha,
this.imsak,
this.midnight,
});
final Fajr fajr;
final String sunrise;
final String dhuhr;
final String asr;
final String sunset;
final String maghrib;
final String isha;
final Imsak imsak;
final String midnight;
factory Timings.fromJson(String str) => Timings.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Timings.fromMap(Map<String, dynamic> json) => Timings(
fajr: json["Fajr"] == null ? null : fajrValues.map[json["Fajr"]],
sunrise: json["Sunrise"] == null ? null : json["Sunrise"],
dhuhr: json["Dhuhr"] == null ? null : json["Dhuhr"],
asr: json["Asr"] == null ? null : json["Asr"],
sunset: json["Sunset"] == null ? null : json["Sunset"],
maghrib: json["Maghrib"] == null ? null : json["Maghrib"],
isha: json["Isha"] == null ? null : json["Isha"],
imsak: json["Imsak"] == null ? null : imsakValues.map[json["Imsak"]],
midnight: json["Midnight"] == null ? null : json["Midnight"],
);
Map<String, dynamic> toMap() => {
"Fajr": fajr == null ? null : fajrValues.reverse[fajr],
"Sunrise": sunrise == null ? null : sunrise,
"Dhuhr": dhuhr == null ? null : dhuhr,
"Asr": asr == null ? null : asr,
"Sunset": sunset == null ? null : sunset,
"Maghrib": maghrib == null ? null : maghrib,
"Isha": isha == null ? null : isha,
"Imsak": imsak == null ? null : imsakValues.reverse[imsak],
"Midnight": midnight == null ? null : midnight,
};
}
enum Fajr { THE_0219_BST, THE_0218_BST, THE_0220_BST, THE_0221_BST, THE_0222_BST }
final fajrValues = EnumValues({
"02:18 (BST)": Fajr.THE_0218_BST,
"02:19 (BST)": Fajr.THE_0219_BST,
"02:20 (BST)": Fajr.THE_0220_BST,
"02:21 (BST)": Fajr.THE_0221_BST,
"02:22 (BST)": Fajr.THE_0222_BST
});
enum Imsak { THE_0209_BST, THE_0208_BST, THE_0210_BST, THE_0211_BST, THE_0212_BST }
final imsakValues = EnumValues({
"02:08 (BST)": Imsak.THE_0208_BST,
"02:09 (BST)": Imsak.THE_0209_BST,
"02:10 (BST)": Imsak.THE_0210_BST,
"02:11 (BST)": Imsak.THE_0211_BST,
"02:12 (BST)": Imsak.THE_0212_BST
});
class EnumValues<T> {
Map<String, T> map;
Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
你只能使用那些需要的类。