Просто проверьте измененный код:
Ниже указан локальный json, который вы указали:
{
"Delhi": {
"districtData": {
"East Delhi": {
"confirmed": 1,
"lastupdatedtime": ""
},
"South West Delhi": {
"confirmed": 3,
"lastupdatedtime": ""
},
"West Delhi": {
"confirmed": 2,
"lastupdatedtime": ""
},
"North Delhi": {
"confirmed": 3,
"lastupdatedtime": ""
},
"New Delhi": {
"confirmed": 3,
"lastupdatedtime": ""
},
"South Delhi": {
"confirmed": 2,
"lastupdatedtime": ""
},
"North East Delhi": {
"confirmed": 1,
"lastupdatedtime": ""
},
"North West Delhi": {
"confirmed": 3,
"lastupdatedtime": ""
},
"Unknown": {
"confirmed": 53,
"lastupdatedtime": ""
},
"Delhi": {
"confirmed": 1,
"lastupdatedtime": ""
}
}
}
}
Позже я создал класс модели для этого:
// To parse this JSON data, do
//
// final indiaState = indiaStateFromJson(jsonString);
import 'dart:convert';
IndiaState indiaStateFromJson(String str) => IndiaState.fromJson(json.decode(str));
String indiaStateToJson(IndiaState data) => json.encode(data.toJson());
class IndiaState {
Delhi delhi;
IndiaState({
this.delhi,
});
factory IndiaState.fromJson(Map<String, dynamic> json) => IndiaState(
delhi: Delhi.fromJson(json["Delhi"]),
);
Map<String, dynamic> toJson() => {
"Delhi": delhi.toJson(),
};
}
class Delhi {
Map<String, DistrictDatum> districtData;
Delhi({
this.districtData,
});
factory Delhi.fromJson(Map<String, dynamic> json) => Delhi(
districtData: Map.from(json["districtData"]).map((k, v) => MapEntry<String, DistrictDatum>(k, DistrictDatum.fromJson(v))),
);
Map<String, dynamic> toJson() => {
"districtData": Map.from(districtData).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())),
};
}
class DistrictDatum {
int confirmed;
String lastupdatedtime;
DistrictDatum({
this.confirmed,
this.lastupdatedtime,
});
factory DistrictDatum.fromJson(Map<String, dynamic> json) => DistrictDatum(
confirmed: json["confirmed"],
lastupdatedtime: json["lastupdatedtime"],
);
Map<String, dynamic> toJson() => {
"confirmed": confirmed,
"lastupdatedtime": lastupdatedtime,
};
}
ниже приведен основной файл, в котором я показал ваши данные в виде списка, проверьте его:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sample_project_for_api/model.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool isLoading = false;
List<Cities> citilesList = List();
// here i have taken the json locally which you posted on stack
Future<String> loadFromAssets() async {
return await rootBundle.loadString('json/parse.json');
}
@override
void initState() {
super.initState();
givenFunction();
}
Future givenFunction() async {
setState(() {
isLoading = true;
});
//final httpRequest = await http.get(districtAPI);
//final json = jsonDecode(httpRequest.body);
// you can make the http call above just uncomment is and comment the below line
String jsonString = await loadFromAssets();
// Here you can just replace down your httpRequest.body with jsonString
final indiaState = indiaStateFromJson(jsonString);
indiaState.delhi.districtData.forEach((key, value) {
// This column is show you the values are getting fetched and printed below
print('This is the key : ' + key);
print('Confirmed :' + value.confirmed.toString());
citilesList.add(Cities(key, value.confirmed));
});
setState(() {
isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: isLoading
? CircularProgressIndicator()
: ListView.builder(
itemCount: citilesList.length,
itemBuilder: (BuildContext context, int index) {
return Container(
child: Card(
margin:
EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Text(citilesList[index].state + ": "),
Text(citilesList[index].confirmed.toString())
],
),
),
),
);
},
),
),
);
}
}
void main() {
runApp(HomePage());
}
class Cities {
final String state;
final int confirmed;
Cities(this.state, this.confirmed);
}
Только что добавил образец изображения для элементов выборки данных: Дайте мне знать, если это работает