gpt4 book ai didi

flutter - 将项目映射到嵌套数组中并将其存储在列表中

转载 作者:行者123 更新时间:2023-12-03 03:31:01 24 4
gpt4 key购买 nike

这是我的代码

 final List<Dummy> loadedItems = [];
//dummy data
final requiredData = {"vendor":"Avatar" ,"grocery_category":[{"category_name":"Fruits", "sub_categories":[{"category_name":"Apple", "items":[{"item_name":"redApple", "item_price":"250"}]}]}]};

loadedItems.add(Dummy(
title: requiredData['vendor'],
subtitle: (requiredData['grocery_category'] as List<dynamic>).map((item) {
Dummy2(item['category_name']);
}).toList()
));
楷模
class Dummy {
final String title;
final List<Dummy2> subtitle;

Dummy({this.title, this.subtitle});
}

class Dummy2 {
final String subTitle;
Dummy2(this.subTitle);
}
如果我这样打印
print(loadedItems[0].title);
输出是
Avatar
同样,如果我这样打印,
print(loadedItems[0].subtitle);
输出是
null
我不知道我在做什么错...到目前为止,我已经尝试过,但是找不到为什么会这样。请给我一个解决方案。提前致谢!

最佳答案

您提供的json:

{
"vendor": "Avatar",
"grocery_category": [{
"category_name": "Fruits",
"sub_categories": [{
"category_name": "Apple",
"items": [{
"item_name": "redApple",
"item_price": "250"
}]
}]
}]
}
查看此模型以获取json:
// To parse this JSON data, do
//
// final groceryModel = groceryModelFromJson(jsonString);

import 'dart:convert';

GroceryModel groceryModelFromJson(String str) => GroceryModel.fromJson(json.decode(str));

String groceryModelToJson(GroceryModel data) => json.encode(data.toJson());

class GroceryModel {
GroceryModel({
this.vendor,
this.groceryCategory,
});

String vendor;
List<GroceryCategory> groceryCategory;

factory GroceryModel.fromJson(Map<String, dynamic> json) => GroceryModel(
vendor: json["vendor"],
groceryCategory: List<GroceryCategory>.from(json["grocery_category"].map((x) => GroceryCategory.fromJson(x))),
);

Map<String, dynamic> toJson() => {
"vendor": vendor,
"grocery_category": List<dynamic>.from(groceryCategory.map((x) => x.toJson())),
};
}

class GroceryCategory {
GroceryCategory({
this.categoryName,
this.subCategories,
});

String categoryName;
List<SubCategory> subCategories;

factory GroceryCategory.fromJson(Map<String, dynamic> json) => GroceryCategory(
categoryName: json["category_name"],
subCategories: List<SubCategory>.from(json["sub_categories"].map((x) => SubCategory.fromJson(x))),
);

Map<String, dynamic> toJson() => {
"category_name": categoryName,
"sub_categories": List<dynamic>.from(subCategories.map((x) => x.toJson())),
};
}

class SubCategory {
SubCategory({
this.categoryName,
this.items,
});

String categoryName;
List<Item> items;

factory SubCategory.fromJson(Map<String, dynamic> json) => SubCategory(
categoryName: json["category_name"],
items: List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
);

Map<String, dynamic> toJson() => {
"category_name": categoryName,
"items": List<dynamic>.from(items.map((x) => x.toJson())),
};
}

class Item {
Item({
this.itemName,
this.itemPrice,
});

String itemName;
String itemPrice;

factory Item.fromJson(Map<String, dynamic> json) => Item(
itemName: json["item_name"],
itemPrice: json["item_price"],
);

Map<String, dynamic> toJson() => {
"item_name": itemName,
"item_price": itemPrice,
};
}

主页
import 'package:flutter/material.dart';
import 'package:json_parsing_example/models.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: Login());
}
}

class Login extends StatefulWidget {
@override
_LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
List<Grocery> items = List();

@override
void initState() {
super.initState();
getData();
}

getData() async {
String data =
await DefaultAssetBundle.of(context).loadString("json/parse.json");

final grocery = groceryFromJson(data);

items.add(grocery);
print(items[0].vendor);
print(items[0].groceryCategory[0].categoryName);
print(items[0].groceryCategory[0].subCategories[0].categoryName);
print(items[0].groceryCategory[0].subCategories[0].items[0].itemName);
print(items[0].groceryCategory[0].subCategories[0].items[0].itemPrice);
}

@override
Widget build(BuildContext context) {
return Scaffold(body: Text('Example'));
}
}

让我知道是否有效。您可以访问其中的项目。

关于flutter - 将项目映射到嵌套数组中并将其存储在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63856916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com