我從服務器收到帶有大量嵌套的回應,但我無法顯示帶有動態元素的第二個嵌套。例如,像這樣 -Text('Child header name - ${catalogDrawer[index].one[index].name}'),
但是,如果將索引替換為從 0 到 1 的數字,則不會有錯誤 -Text('Child header name - ${catalogDrawer[index].one[0].name}'),
此外,我可以輕松顯示第一級資料 -Text('Header name - ${catalogDrawer[index].name}'),
但是,嵌套中的所有內容都會降低是一場災難。我該怎么辦?
這是來自服務器的回應 -
"categories": [
{
"name": "Dlya nego",
"url": "https://dev.radiwear.com/api/v1/m/catalog/dlya-nego/",
"children": [
{
"name": "obyv",
"url": "https://dev.radiwear.com/api/v1/m/catalog/dlya-nego/obuv/",
"children": [
{
"name": "Krossovki",
"url": "https://dev.radiwear.com/api/v1/m/catalog/dlya-nego/obuv/krossovki/",
"children": []
},
{
"name": "slancy",
"url": "https://dev.radiwear.com/api/v1/m/catalog/dlya-nego/obuv/slancy/",
"children": []
}
]
},
{
"name": "Kurtki",
"url": "https://dev.radiwear.com/api/v1/m/catalog/dlya-nego/Kurtki/",
"children": [
{
"name": "bombery",
"url": "https://dev.radiwear.com/api/v1/m/catalog/dlya-nego/Kurtki/bombery/",
"children": []
}
]
}
]
},
{
"name": "dlya nee",
"url": "https://dev.radiwear.com/api/v1/m/catalog/dlya-nee/",
"children": [
{
"name": "obyv",
"url": "https://dev.radiwear.com/api/v1/m/catalog/dlya-nee/obuv/",
"children": [
{
"name": "Krossovki",
"url": "https://dev.radiwear.com/api/v1/m/catalog/dlya-nee/obuv/krossovki/",
"children": []
}
]
}
]
},
{
"name": "deti",
"url": "https://dev.radiwear.com/api/v1/m/catalog/deti/",
"children": [
{
"name": "obuv",
"url": "https://dev.radiwear.com/api/v1/m/catalog/deti/obuv/",
"children": [
{
"name": "krossovki",
"url": "https://dev.radiwear.com/api/v1/m/catalog/deti/obuv/krossovki/",
"children": []
}
]
}
]
},
{
"name": "Sale",
"url": "https://dev.radiwear.com/api/v1/m/catalog/sale/",
"children": [
{
"name": "obuv",
"url": "https://dev.radiwear.com/api/v1/m/catalog/sale/obuv/",
"children": [
{
"name": "Krossovki",
"url": "https://dev.radiwear.com/api/v1/m/catalog/sale/obuv/krossovki/",
"children": []
}
]
}
]
}
],
這是我的物體 -
class CatalogDrawerEntity extends Equatable {
final String name;
final String url;
final List<OneChildren> one;
const CatalogDrawerEntity ({
required this.name,
required this.url,
required this.one
});
@override
// TODO: implement props
List<Object?> get props => [name, url, one];
}
class OneChildren {
final String name;
final String url;
final List<TwoChildren> two;
const OneChildren({
required this.name,
required this.url,
required this.two
});
}
class TwoChildren {
final String name;
final String url;
TwoChildren({
required this.name,
required this.url
});
}
這是我的模型-
class CatalogDrawerModel extends CatalogDrawerEntity {
const CatalogDrawerModel({
required name,
required url,
required one
}) : super (
name: name,
url: url,
one: one
);
factory CatalogDrawerModel.fromJson(Map<String, dynamic> json) {
return CatalogDrawerModel(
name: json['name'],
url: json['url'],
one: json['children'] != null
? (json['children'] as List<dynamic>).map((json) => OneModel.fromJson(json)).toList()
: null
);
}
Map<String, dynamic> toJson() {
return {
'name': name,
'url': url,
'children': one
};
}
}
和
class OneModel extends OneChildren{
OneModel({name, url, two}) : super (name: name, url: url, two: two);
factory OneModel.fromJson(Map<String, dynamic> json) {
return OneModel(
name: json['name'],
url: json['url'],
two: json['children'] != null
? (json['children'] as List<dynamic>).map((json) => TwoModel.fromJson(json)).toList()
: null
);
}
Map<String, dynamic> toJson() {
return {
'name': name,
'url': url,
'two': two
};
}
}
class TwoModel extends TwoChildren{
TwoModel({name, url}) : super (name: name, url: url);
factory TwoModel.fromJson(Map<String, dynamic> json) {
return TwoModel(
name: json['name'],
url: json['url']
);
}
Map<String, dynamic> toJson() {
return {
'name': name,
'url': url
};
}
}
這是來自服務器的資料處理 -
Future<List<CatalogDrawerModel>> getAllCatalogDrawer() async {
final response = await client.get(
Uri.parse(ConfigUrl.home),
headers: {'Content-Type': 'applications/json'}
);
if(response.statusCode == 200) {
List catalogDrawer = json.decode(response.body)['categories'];
return catalogDrawer.map((data) => CatalogDrawerModel.fromJson(data)).toList();
// final catalogDrawer= json.decode(response.body);
// print('Данные для карточек - $catalogDrawer');
// return (catalogDrawer['categories'] as List).map((e) => CatalogDrawerModel.fromJson(e)).toList();
} else {
throw ServerException();
}
}
uj5u.com熱心網友回復:
您決議它的方式是正確的,但您顯示它的方式是錯誤的,您不能使用相同的catalogDrawerfor索引one,試試這個:
ListView.builder(
itemBuilder: (context, index) {
var one = catalogDrawer[index].one;
return ListView.builder(
itemBuilder: (context, i) {
var two = one[i].two;
return ListView.builder(
itemBuilder: (context, ii) {
return Text('Child header name - ${two[ii].name}');
},
itemCount: two.length,
shrinkWrap: true,
);
},
itemCount: one.length,
shrinkWrap: true,
);
},
itemCount: checkboxes.length,
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529382.html
標籤:扑镖颤振布局
