运行项目时出现错误。我得到的错误是
Type 'String' is not a subtype of expected type 'int' of index
有人可以告诉我我做错了什么。我一直在寻找解决方案,并找到了一些尝试过但未能解决问题的解决方案。
这是我尝试过的第一个解决方案,但给出了相同的错误。
factory Prayer.fromJson(Map<String, dynamic> json){
return Prayer(
fajr: json['data'][0]['timings']['Fajr'].toString(),
duhr: json['data'][0]['timings']['Dhuhr'].toString(),
asr: json['data'][0]['timings']['Asr'].toString(),
maghrib: json['data'][0]['timings']['Maghrib'].toString(),
isha: json['data'][0]['timings']['Isha'].toString()
);
}
这是我找到的第二个解决方案,但没有解决我的问题
factory Prayer.fromJson(Map<String, dynamic> json){
return Prayer(
fajr: json['Fajr'].toString(),
duhr: json['Dhuhr'].toString(),
asr: json['Asr'].toString(),
maghrib: json['Maghrib'].toString(),
isha: json['Isha'].toString()
);
}
这是我的代码:
prayerpage.dart
FutureBuilder<List<Prayer>>(
future: fetchPrayers(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Prayer> prayer = snapshot.data;
return Column(
children: prayer.map((p) => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(p.fajr),
Text(p.duhr),
Text(p.asr),
Text(p.maghrib),
Text(p.isha),
Text('ok'),
])).toList(),
);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
}
return CircularProgressIndicator();
})
祈祷
class Prayer {
String fajr;
String duhr;
String asr;
String maghrib;
String isha;
Prayer({this.fajr,this.duhr,this.asr,this.maghrib,this.isha});
factory Prayer.fromJson(Map<String, dynamic> json){
return Prayer(
fajr: json['data']['timings']['Fajr'].toString(),
duhr: json['data']['timings']['Dhuhr'].toString(),
asr: json['data']['timings']['Asr'].toString(),
maghrib: json['data']['timings']['Maghrib'].toString(),
isha: json['data']['timings']['Isha'].toString()
);
}
}
提取祈祷
Future<List<Prayer>> fetchPrayers() async {
final response = await http.get(
'http://api.aladhan.com/v1/calendarByCity?city=Antwerpen&country=Belgium&method=2&month=03&year=2020');
var responsejson = json.decode(response.body);
if (response.statusCode == 200) {
return (responsejson['data']['timings'] as List)
.map((p) => Prayer.fromJson(p))
.toList();
} else {
throw Exception('Failed to load Prayers');
}
}
这是JSON文件
HTTP/1.1 200 OK
{
"code": 200,
"status": "OK",
"data": [{
"timings": {
"Fajr": "03:57",
"Sunrise": "05:46",
"Dhuhr": "12:59",
"Asr": "16:55",
"Sunset": "20:12",
"Maghrib": "20:12",
"Isha": "22:02",
"Imsak": "03:47",
"Midnight": "00:59"
},
"date": {
"readable": "24 Apr 2014",
"timestamp": "1398332113",
"gregorian": {
"date": "15-05-2018",
"format": "DD-MM-YYYY",
"day": "15",
"weekday": {
"en": "Tuesday"
},
"month": {
"number": 5,
"en": "May",
},
"year": "2018",
"designation": {
"abbreviated": "AD",
"expanded": "Anno Domini",
},
},
"hijri": {
"date": "01-09-1439",
"format": "DD-MM-YYYY",
"day": "01",
"weekday": {
"en": "Al Thalaata",
"ar": "الثلاثاء",
},
"month": {
"number": 9,
"en": "Ramaḍān",
"ar": "رَمَضان",
},
"year": "1439",
"designation": {
"abbreviated": "AH",
"expanded": "Anno Hegirae",
},
"holidays": [
"1st Day of Ramadan"
],
},
},
答案 0 :(得分:0)
尝试一下
此为您 fetchPrayers
Future<List<Prayer>> fetchPrayers() async {
final response = await http.get(
'http://api.aladhan.com/v1/calendarByCity?city=Antwerpen&country=Belgium&method=2&month=03&year=2020');
var responsejson = json.decode(response.body);
if (response.statusCode == 200) {
List<Map<String, Map<String, Object>>> data = responsejson["data"] as List<Map<String, Map<String, Object>>> ;
return data.map((element) => Prayer.fromJson(element["timings"])).toList();
} else {
throw Exception('Failed to load Prayers');
}
}
并将其用于您的 Prayer.fromJson 工厂构造函数
factory Prayer.fromJson(Map<String, dynamic> json){
return Prayer(
fajr: json['Fajr'].toString(),
duhr: json['Dhuhr'].toString(),
asr: json['Asr'].toString(),
maghrib: json['Maghrib'].toString(),
isha: json['Isha'].toString()
);
}
希望对您有帮助