我收到如下回复
{
"success": true,
"data": {
"store 1": [
{
"id": 27,
"cart_id": 5,
"item": {
"id": 35,
"name": "test prod 1"
}
},
{
"id": 28,
"cart_id": 56,
"item": {
"id": "jk",
"name": "test prod 2"
}
},
{
"id": 27,
"cart_id": 5,
"item": {
"id": 35,
"name": "test prod 1"
}
}
],
"store 2": [
{
"id": 27,
"cart_id": 5,
"item": {
"id": 35,
"name": "test prod 1"
}
},
{
"id": 28,
"cart_id": 56,
"item": {
"id": "jk",
"name": "test prod 2"
}
},
{
"id": 27,
"cart_id": 5,
"item": {
"id": 35,
"name": "test prod 1"
}
}
],
"store 3": [
{
"id": 27,
"cart_id": 5,
"item": {
"id": 35,
"name": "test prod 1"
}
},
{
"id": 28,
"cart_id": 56,
"item": {
"id": "jk",
"name": "test prod 2"
}
},
{
"id": 27,
"cart_id": 5,
"item": {
"id": 35,
"name": "test prod 1"
}
}
]
}
}
这是我如何处理 api 响应中的值
if (response.data['success'] == true) {
Map mapValue = jsonDecode(response.data);
List<ShoppingCartModel> cartList = [];
mapValue['data'].forEach((key, value) {
List<Store> store = [];
value.forEach((item) {
store.add(Store(
id: item['id'],
cartid: item['cart_id'],
product: item['product']));
});
cartList.add(ShoppingCartModel(sname: key, storeList: store));
});
print(cartList);
return cartList;
这是我的模型
class ShoppingCartModel {
String sname;
List<Store> storeList;
ShoppingCartModel({this.sname, this.storeList});
ShoppingCartModel.fromJson(Map<String, dynamic> json) {
sname = json['sname'];
storeList =
List<Store>.from(json["storeList"].map((x) => Store.fromJson(x)));
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['sname'] = this.sname;
data['storeList'] = List<dynamic>.from(storeList.map((x) => x.toJson()));
return data;
}
}
class Store {
int id;
int cartid;
Item item;
Store({this.id, this.cartid, this.item});
Store.fromJson(Map<String, dynamic> json) {
id = json['id'];
cartid = json['cart_id'];
item = Item.fromJson(json["item"]);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['cart_id'] = this.cartid;
data['item'] = item.toJson();
return data;
}
}
class Item {
int id;
String name;
Item({
this.id,
this.name,
});
Item.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
//price = json['price'].toDouble();
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
return data;
}
}
这是我的部分代码
return ListView(
physics: AlwaysScrollableScrollPhysics(),
children: [
Container(
padding: EdgeInsets.all(16),
color: Colors.white,
child: Column(
children: List.generate(shoppingCartData.length,
(index) {
return _buildStoreName(index, boxImageSize);
}),
),
),
Container(
margin: EdgeInsets.only(top: 12),
padding: EdgeInsets.all(16),
color: Colors.white,
child: Column(
children: [
],
),
)
],
);
}
Column _buildStoreName(index, boxImageSize) {
//int quantity = shoppingCartData[index].qty;
return Column(
children: [
Container(
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
shoppingCartData[index].sname,
style: GlobalStyle.productName.copyWith(fontSize: 14),
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
Expanded(
child: Column(
children: List.generate(
shoppingCartData[index].storeList.length, (indexStore) {
return _buildItem(shoppingCartData[index].storeList,
indexStore, boxImageSize);
}),
),
)
],
),
),
),
],
);
}
Column _buildItem(cartdata, indexStore, boxImageSize) {
int quantity = cartdata[indexStore].item.quantity;
return Column(
children: [
Container(
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
GestureDetector(
onTap: () {
),
SizedBox(
width: 10,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
GestureDetector(
onTap: () {
},
child: Text(
cartdata[indexStore].item.name,
style: GlobalStyle.productName.copyWith(fontSize: 14),
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
),
],
),
)
],
),
),
),
)
],
);
}
这是我需要的视图
我在映射时出错。只是商店名称不同,但里面的键相同
我需要在列表视图中迭代第一个列表作为商店和第二个列表作为 iem 值
答案 0 :(得分:0)
在 response.data
之前,您无法将 decode
作为地图访问。
在此条件之前尝试jsonDecode()
if (response.data['success'] == true)
示例:
var res = jsonDecode(response.data);
if(res['success'] == true){
// do rest of your operation
}
答案 1 :(得分:0)
我正在为此代码使用 Get 包:
products_data.dart
const String productData = '''
{
"success": true,
"data": {
...
}
}
''';
item_model.dart
import 'package:flutter/foundation.dart';
class ItemModel {
dynamic id;
String name;
ItemModel({@required this.id, @required this.name});
ItemModel.fromJson(Map<String, dynamic> data) {
id = data['id'];
name = data['name'];
}
}
asset_model.dart
import 'package:flutter/material.dart';
import 'package:getx_json/models/item_model.dart';
class AssetModel {
int id;
int cartId;
ItemModel item;
AssetModel({@required this.id, @required this.cartId, @required this.item});
AssetModel.fromJson(Map<String, dynamic> data) {
id = data['id'];
cartId = data['cart_id'];
item = ItemModel.fromJson(data['item']);
}
Card showCard() => Card(
color: Colors.lightBlue,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('id: $id'),
Text('cart_id: $cartId'),
Text('Item', style: TextStyle(fontWeight: FontWeight.bold)),
Container(
width: double.infinity,
child: Padding(
padding: const EdgeInsets.only(left: 10, right: 10),
child: Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('id: ${item.id}'),
Text('name: ${item.name}'),
],
),
),
),
),
],
),
);
}
shopping_controller.dart
import 'dart:convert';
import 'package:get/get.dart';
import 'package:getx_json/models/asset_model.dart';
import 'package:getx_json/src/shared/products_data.dart';
class ShoppingController extends GetxController {
var assets = Map<String, List<AssetModel>>().obs;
@override
void onInit(){
super.onInit();
fetchProducts();
}
void fetchProducts() async {
await Future.delayed(Duration(seconds: 1));
Map<String, dynamic> response = jsonDecode(productData);
if (response['success'] == true) {
var data = Map<String, List<AssetModel>>();
(response['data'] as Map<String, dynamic>).forEach((key, value) {
data[key] = (value as List).map((e) => AssetModel.fromJson(e)).toList();
});
assets.addAll(data);
}
}
}
home_page.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_json/src/controllers/shopping_controller.dart';
class HomePage extends StatelessWidget {
/* ---------------------------------------------------------------------------- */
const HomePage({Key key}) : super(key: key);
/* ---------------------------------------------------------------------------- */
@override
Widget build(BuildContext context) {
final stores = Get.put(ShoppingController());
return Scaffold(
body: SafeArea(
child:
Obx(() => stores.assets.length > 0 ? buildListView() : buildText()),
),
);
}
Widget buildListView() {
final stores = Get.find<ShoppingController>().assets;
final titles = stores.keys.toList();
return ListView.builder(
physics: ScrollPhysics(),
scrollDirection: Axis.vertical,
itemCount: stores.length,
itemBuilder: (context, index) => Card(
color: Colors.orange,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Text(titles[index], style: TextStyle(fontWeight: FontWeight.bold)),
ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: stores[titles[index]].length,
itemBuilder: (context, idx) => stores[titles[index]][idx].showCard(),
),
],
),
),
),
);
}
Widget buildText() {
final stores = Get.find<ShoppingController>();
return Text(stores.assets.toString());
}
}