Вы можете скопировать и вставить полный код ниже
Вы можете использовать преобразование map
в list
с payload.data.forEach((k, v) => list.add(v));
и удалить управляющий символ \t
и отобразить с FutureBuilder
фрагмент кода
var response = http.Response(jsonString, 200);
List<Address> list = [];
if (response.statusCode == 200) {
String jsonStringNoCtrlChar = response.body.replaceAll("\t", "");
var payload = payloadFromJson(jsonStringNoCtrlChar);
payload.data.forEach((k, v) => list.add(v));
return list;
}
рабочая демонстрация
полный код
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));
String payloadToJson(Payload data) => json.encode(data.toJson());
class Payload {
int success;
List<dynamic> error;
Map<String, Address> data;
Payload({
this.success,
this.error,
this.data,
});
factory Payload.fromJson(Map<String, dynamic> json) => Payload(
success: json["success"],
error: List<dynamic>.from(json["error"].map((x) => x)),
data: Map.from(json["data"])
.map((k, v) => MapEntry<String, Address>(k, Address.fromJson(v))),
);
Map<String, dynamic> toJson() => {
"success": success,
"error": List<dynamic>.from(error.map((x) => x)),
"data": Map.from(data)
.map((k, v) => MapEntry<String, dynamic>(k, v.toJson())),
};
}
class Address {
String addressId;
String firstname;
String lastname;
String company;
String address1;
String address2;
String postcode;
String city;
String zoneId;
String zone;
String zoneCode;
String countryId;
String country;
String isoCode2;
String isoCode3;
String addressFormat;
dynamic customField;
Address({
this.addressId,
this.firstname,
this.lastname,
this.company,
this.address1,
this.address2,
this.postcode,
this.city,
this.zoneId,
this.zone,
this.zoneCode,
this.countryId,
this.country,
this.isoCode2,
this.isoCode3,
this.addressFormat,
this.customField,
});
factory Address.fromJson(Map<String, dynamic> json) => Address(
addressId: json["address_id"],
firstname: json["firstname"],
lastname: json["lastname"],
company: json["company"],
address1: json["address_1"],
address2: json["address_2"],
postcode: json["postcode"],
city: json["city"],
zoneId: json["zone_id"],
zone: json["zone"],
zoneCode: json["zone_code"],
countryId: json["country_id"],
country: json["country"],
isoCode2: json["iso_code_2"],
isoCode3: json["iso_code_3"],
addressFormat: json["address_format"],
customField: json["custom_field"],
);
Map<String, dynamic> toJson() => {
"address_id": addressId,
"firstname": firstname,
"lastname": lastname,
"company": company,
"address_1": address1,
"address_2": address2,
"postcode": postcode,
"city": city,
"zone_id": zoneId,
"zone": zone,
"zone_code": zoneCode,
"country_id": countryId,
"country": country,
"iso_code_2": isoCode2,
"iso_code_3": isoCode3,
"address_format": addressFormat,
"custom_field": customField,
};
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<List<Address>> _future;
Future<List<Address>> getAddresList() async {
try {
/*var response = await http.post(
"URL",
headers: {"content-type": "application/json", "cookie": cookie});*/
String jsonString = '''
{"success":1,
"error":[],
"data":
{"38":
{"address_id":"38",
"firstname":"Raj",
"lastname":"s",
"company":"",
"address_1":"aaaa",
"address_2":"",
"postcode":"966666",
"city":"aa",
"zone_id":"1234",
"zone":"Kerewan",
"zone_code":"KE",
"country_id":"0",
"country":"",
"iso_code_2":"",
"iso_code_3":"",
"address_format":"",
"custom_field":null},
"37":{"address_id":"37","firstname":"Raj","lastname":"s","company":"","address_1":"4 kk\t","address_2":"","postcode":"56774\t","city":"Chennai\t","zone_id":"1234","zone":"Kerewan","zone_code":"KE","country_id":"0","country":"","iso_code_2":"","iso_code_3":"","address_format":"","custom_field":null}}
}
''';
var response = http.Response(jsonString, 200);
List<Address> list = [];
if (response.statusCode == 200) {
String jsonStringNoCtrlChar = response.body.replaceAll("\t", "");
var payload = payloadFromJson(jsonStringNoCtrlChar);
payload.data.forEach((k, v) => list.add(v));
return list;
}
} catch (err, trace) {
print(trace.toString());
print(err.toString());
rethrow;
}
}
@override
void initState() {
_future = getAddresList();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder(
future: _future,
builder: (context, AsyncSnapshot<List<Address>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('none');
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
case ConnectionState.active:
return Text('');
case ConnectionState.done:
if (snapshot.hasError) {
return Text(
'${snapshot.error}',
style: TextStyle(color: Colors.red),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Card(
elevation: 6.0,
child: Padding(
padding: const EdgeInsets.only(
top: 6.0,
bottom: 6.0,
left: 8.0,
right: 8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(snapshot.data[index].addressId),
Spacer(),
Text(snapshot.data[index].address1),
],
),
));
});
}
}
}));
}
}