- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在使用来自REST服务的此API,该服务以以下json格式向我发送数据。
API响应
[
{
"id": 7,
"daily_allowance_actual_percentage": {
"Bone": [
0.01,
"Red"
],
"Muscle Meat": [
46.15,
"Red"
],
"Fruit and Veg": [
53.84,
"Red"
]
},
"daily_calories_from_this_meal_plan": 823.25,
"imperial_weight_of_one_meal": 5.732025,
"meal_ingredients_would_make": 8,
"meals_per_day": 2,
"nutrient_value": [
{
"Calcium": [
249.5,
"mg",
"Yellow",
0.008800863,
"ounce"
]
},
{
"Choline": [
203.65,
"mg",
"Green",
0.0071835501,
"ounce"
]
},
{
"Copper": [
1.33,
"mg",
"Green",
0.00004691442,
"ounce"
]
},
{
"Crude fat": [
29.75,
"g",
"Green",
1.0494015,
"ounce"
]
},
{
"Folate": [
469,
"mcg",
"Green",
0.000016543506,
"ounce"
]
},
{
"Iodine": [
2.1,
"mcg",
"Red",
7.40754e-8,
"ounce"
]
},
{
"Iron": [
11.77,
"mg",
"Green",
0.00041517497999999997,
"ounce"
]
},
{
"Magnesium": [
253,
"mg",
"Red",
0.008924322,
"ounce"
]
},
{
"Manganese": [
1.82,
"mg",
"Green",
0.00006419868,
"ounce"
]
},
{
"Niacin (B3)": [
11.88,
"mg",
"Green",
0.00041905512,
"ounce"
]
},
{
"Omega-3 excl. ALA and SDA": [
0.03,
"g",
"Green",
0.00105822,
"ounce"
]
},
{
"Omega-6": [
0.93,
"g",
"Green",
0.03280482,
"ounce"
]
},
{
"Pantothenic acid (B5)": [
1.7,
"mg",
"Green",
0.000059965799999999994,
"ounce"
]
},
{
"Phosphorus": [
800,
"mg",
"Yellow",
0.0282192,
"ounce"
]
},
{
"Potassium": [
2766.5,
"mg",
"Red",
0.09758552100000001,
"ounce"
]
},
{
"Protein": [
56.02,
"g",
"Green",
1.9760494800000001,
"ounce"
]
},
{
"Riboflavin (B2)": [
0.54,
"mg",
"Green",
0.00001904796,
"ounce"
]
},
{
"Selenium": [
39.05,
"mcg",
"Red",
0.0000013774497,
"ounce"
]
},
{
"Sodium (Na)": [
123,
"mg",
"Green",
0.004338702,
"ounce"
]
},
{
"Thiamin (B1)": [
0.78,
"mg",
"Green",
0.00002751372,
"ounce"
]
},
{
"Vitamin A": [
4.2,
"mcg",
"Red",
1.481508e-7,
"ounce"
]
},
{
"Vitamin C": [
1.75,
"mg",
"Green",
0.0000617295,
"ounce"
]
},
{
"Vitamin D": [
0.15,
"mcg",
"Red",
5.2911000000000005e-9,
"ounce"
]
},
{
"Vitamin E": [
1.03,
"mg",
"Red",
0.000036332220000000006,
"ounce"
]
},
{
"Zinc (Zn)": [
8.24,
"mg",
"Green",
0.00029065776000000005,
"ounce"
]
},
{
"Calories": [
823.25,
"cal",
null,
"N/A",
"ounce"
]
}
],
"pet_name": "grover",
"show_weight_of_balanced_meal": false,
"suggestion": {
"add_food": {
"Yellow": [
{
"Calcium": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Phosphorus": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
}
],
"Red": [
{
"Iodine": [
"Amaranth, grain, whole, uncooked",
"Apricot, dried"
]
},
{
"Vitamin A": [
"Bean, edamame, from frozen, cooked",
"Beef Brain"
]
},
{
"Vitamin D": [
"Beef kidney, raw",
"Beef liver, raw"
]
},
{
"Vitamin E": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Choline": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Copper": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Crude fat": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Folate": [
"Amaranth, grain, whole, uncooked",
"Apple, fuji, unpeeled, raw"
]
},
{
"Iron": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Manganese": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Niacin (B3)": [
"Apple, fuji, unpeeled, raw",
"Apple, golden delicious, unpeeled, raw"
]
},
{
"Omega-3 excl. ALA and SDA": [
"Bass, fillet, raw",
"Beef Brain"
]
},
{
"Omega-6": [
"Amaranth, grain, whole, uncooked",
"Avocado, raw"
]
},
{
"Pantothenic acid (B5)": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Protein": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Riboflavin (B2)": [
"Amaranth, grain, whole, uncooked",
"Apple, fuji, unpeeled, raw"
]
},
{
"Sodium (Na)": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Thiamin (B1)": [
"Amaranth, grain, whole, uncooked",
"Apple, fuji, unpeeled, raw"
]
},
{
"Zinc (Zn)": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
}
]
},
"remove_food": {
"Yellow": [],
"Red": [
{
"Magnesium": [
"Beef, all cuts, untrimmed, raw, no bone",
"Apricot, dried"
]
},
{
"Potassium": [
"Beef, all cuts, untrimmed, raw, no bone",
"Apricot, dried"
]
},
{
"Selenium": [
"Beef, all cuts, untrimmed, raw, no bone",
"Apricot, dried"
]
}
]
}
},
"total_calories_in_a_meal_plan": 3293,
"total_imperial_weight_of_a_meal_plan": 45.8562,
"total_weight_of_a_meal_plan": 1300,
"weight_of_one_meal": 162.5,
"feeding_plan_details": [
{
"id": 23,
"imperial_quantity": 14.1096,
"quantity": 400,
"food": {
"id": 5,
"bone_percentage": 0,
"calcium_phosphorous_ratio": 0.14,
"name": "Beef, all cuts, untrimmed, raw, no bone",
"omega_ratio": 0,
"omega_ratio_color": "Red",
"calcium_phosphorous_ratio_color": "Red"
}
},
{
"id": 24,
"imperial_quantity": 7.0548,
"quantity": 200,
"food": {
"id": 45,
"bone_percentage": 0,
"calcium_phosphorous_ratio": 0.56,
"name": "Apricot, dried",
"omega_ratio": 0,
"omega_ratio_color": "Red",
"calcium_phosphorous_ratio_color": "Yellow"
}
},
{
"id": 25,
"imperial_quantity": 17.637,
"quantity": 500,
"food": {
"id": 222,
"bone_percentage": 0,
"calcium_phosphorous_ratio": 0.38,
"name": "Bean, haricot, dried",
"omega_ratio": 0,
"omega_ratio_color": "Red",
"calcium_phosphorous_ratio_color": "Red"
}
},
{
"id": 26,
"imperial_quantity": 7.0548,
"quantity": 200,
"food": {
"id": 294,
"bone_percentage": 0,
"calcium_phosphorous_ratio": 0.08,
"name": "Pork, all cuts, lean, raw",
"omega_ratio": 0.09,
"omega_ratio_color": "Yellow",
"calcium_phosphorous_ratio_color": "Red"
}
}
]
}
]
我使用以下方法将来自api响应的值插入到以后需要使用的列表中。
List<AllMealPlanModel> _allMealPlanList = new List<AllMealPlanModel>();
//
Future<List<AllMealPlanModel>> getAllMealPlanData(String item) async {
// await _fetchCategoryData();
//
_mealPlanApiUrl =
'feeding_plans/fetch_all?user_email=$_userEmail&user_token=$_userToken&pet_id=$item';
String _baseUrl = 'https://api.pawfect-balance.oz.to/';
String _fullUrl = _baseUrl + _mealPlanApiUrl;
final response = await CallApi().getData(_mealPlanApiUrl);
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON
List<dynamic> values = new List<dynamic>();
//// error points at here ////
values = json.decode(response.body);
if (values.length > 0) {
for (int i = 0; i < values.length; i++) {
if (values[i] != null) {
Map<String, dynamic> map = values[i];
_allMealPlanList.add(AllMealPlanModel.fromJson(map));
debugPrint('Id-------${map['id']}');
debugPrint('pet_name-------${map['petName']}');
}
}
}
print(_allMealPlanList);
return _allMealPlanList;
} else {
_allMealPlanList = [];
_showSnackbar('A Network Error Occurred.!', errorNet);
return _allMealPlanList;
// If that call was not successful, throw an error.
// throw Exception('Failed to load data');
}
}
在调试器中,当我调试应用程序时,出现以下错误。
Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'
我该如何解决?我的方法做错了什么?有人可以帮我解决这个问题吗?
最佳答案
您可以在下面复制粘贴运行完整代码
对于此错误,您可以使用var values = json.decode(response.body);
为了将来,您可以使用allMealPlanModelFromJson
进行解析,请参见deail的完整代码
并使用FutureBuilder
和嵌套的ListView.separated
显示数据
程式码片段
List<AllMealPlanModel> allMealPlanModelFromJson(String str) =>
List<AllMealPlanModel>.from(
json.decode(str).map((x) => AllMealPlanModel.fromJson(x)));
http.Response response = http.Response(jsonString, 200);
if (response.statusCode == 200) {
return allMealPlanModelFromJson(response.body);
}
工作演示
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
List<AllMealPlanModel> allMealPlanModelFromJson(String str) =>
List<AllMealPlanModel>.from(
json.decode(str).map((x) => AllMealPlanModel.fromJson(x)));
String allMealPlanModelToJson(List<AllMealPlanModel> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class AllMealPlanModel {
AllMealPlanModel({
this.id,
this.dailyAllowanceActualPercentage,
this.dailyCaloriesFromThisMealPlan,
this.imperialWeightOfOneMeal,
this.mealIngredientsWouldMake,
this.mealsPerDay,
this.nutrientValue,
this.petName,
this.showWeightOfBalancedMeal,
this.suggestion,
this.totalCaloriesInAMealPlan,
this.totalImperialWeightOfAMealPlan,
this.totalWeightOfAMealPlan,
this.weightOfOneMeal,
this.feedingPlanDetails,
});
int id;
DailyAllowanceActualPercentage dailyAllowanceActualPercentage;
double dailyCaloriesFromThisMealPlan;
double imperialWeightOfOneMeal;
int mealIngredientsWouldMake;
int mealsPerDay;
List<NutrientValue> nutrientValue;
String petName;
bool showWeightOfBalancedMeal;
Suggestion suggestion;
int totalCaloriesInAMealPlan;
double totalImperialWeightOfAMealPlan;
int totalWeightOfAMealPlan;
double weightOfOneMeal;
List<FeedingPlanDetail> feedingPlanDetails;
factory AllMealPlanModel.fromJson(Map<String, dynamic> json) =>
AllMealPlanModel(
id: json["id"],
dailyAllowanceActualPercentage: DailyAllowanceActualPercentage.fromJson(
json["daily_allowance_actual_percentage"]),
dailyCaloriesFromThisMealPlan:
json["daily_calories_from_this_meal_plan"].toDouble(),
imperialWeightOfOneMeal: json["imperial_weight_of_one_meal"].toDouble(),
mealIngredientsWouldMake: json["meal_ingredients_would_make"],
mealsPerDay: json["meals_per_day"],
nutrientValue: List<NutrientValue>.from(
json["nutrient_value"].map((x) => NutrientValue.fromJson(x))),
petName: json["pet_name"],
showWeightOfBalancedMeal: json["show_weight_of_balanced_meal"],
suggestion: Suggestion.fromJson(json["suggestion"]),
totalCaloriesInAMealPlan: json["total_calories_in_a_meal_plan"],
totalImperialWeightOfAMealPlan:
json["total_imperial_weight_of_a_meal_plan"].toDouble(),
totalWeightOfAMealPlan: json["total_weight_of_a_meal_plan"],
weightOfOneMeal: json["weight_of_one_meal"].toDouble(),
feedingPlanDetails: List<FeedingPlanDetail>.from(
json["feeding_plan_details"]
.map((x) => FeedingPlanDetail.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"id": id,
"daily_allowance_actual_percentage":
dailyAllowanceActualPercentage.toJson(),
"daily_calories_from_this_meal_plan": dailyCaloriesFromThisMealPlan,
"imperial_weight_of_one_meal": imperialWeightOfOneMeal,
"meal_ingredients_would_make": mealIngredientsWouldMake,
"meals_per_day": mealsPerDay,
"nutrient_value":
List<dynamic>.from(nutrientValue.map((x) => x.toJson())),
"pet_name": petName,
"show_weight_of_balanced_meal": showWeightOfBalancedMeal,
"suggestion": suggestion.toJson(),
"total_calories_in_a_meal_plan": totalCaloriesInAMealPlan,
"total_imperial_weight_of_a_meal_plan": totalImperialWeightOfAMealPlan,
"total_weight_of_a_meal_plan": totalWeightOfAMealPlan,
"weight_of_one_meal": weightOfOneMeal,
"feeding_plan_details":
List<dynamic>.from(feedingPlanDetails.map((x) => x.toJson())),
};
}
class DailyAllowanceActualPercentage {
DailyAllowanceActualPercentage({
this.bone,
this.muscleMeat,
this.fruitAndVeg,
});
List<dynamic> bone;
List<dynamic> muscleMeat;
List<dynamic> fruitAndVeg;
factory DailyAllowanceActualPercentage.fromJson(Map<String, dynamic> json) =>
DailyAllowanceActualPercentage(
bone: List<dynamic>.from(json["Bone"].map((x) => x)),
muscleMeat: List<dynamic>.from(json["Muscle Meat"].map((x) => x)),
fruitAndVeg: List<dynamic>.from(json["Fruit and Veg"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"Bone": List<dynamic>.from(bone.map((x) => x)),
"Muscle Meat": List<dynamic>.from(muscleMeat.map((x) => x)),
"Fruit and Veg": List<dynamic>.from(fruitAndVeg.map((x) => x)),
};
}
class FeedingPlanDetail {
FeedingPlanDetail({
this.id,
this.imperialQuantity,
this.quantity,
this.food,
});
int id;
double imperialQuantity;
int quantity;
Food food;
factory FeedingPlanDetail.fromJson(Map<String, dynamic> json) =>
FeedingPlanDetail(
id: json["id"],
imperialQuantity: json["imperial_quantity"].toDouble(),
quantity: json["quantity"],
food: Food.fromJson(json["food"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"imperial_quantity": imperialQuantity,
"quantity": quantity,
"food": food.toJson(),
};
}
class Food {
Food({
this.id,
this.bonePercentage,
this.calciumPhosphorousRatio,
this.name,
this.omegaRatio,
this.omegaRatioColor,
this.calciumPhosphorousRatioColor,
});
int id;
int bonePercentage;
double calciumPhosphorousRatio;
String name;
double omegaRatio;
String omegaRatioColor;
String calciumPhosphorousRatioColor;
factory Food.fromJson(Map<String, dynamic> json) => Food(
id: json["id"],
bonePercentage: json["bone_percentage"],
calciumPhosphorousRatio: json["calcium_phosphorous_ratio"].toDouble(),
name: json["name"],
omegaRatio: json["omega_ratio"].toDouble(),
omegaRatioColor: json["omega_ratio_color"],
calciumPhosphorousRatioColor: json["calcium_phosphorous_ratio_color"],
);
Map<String, dynamic> toJson() => {
"id": id,
"bone_percentage": bonePercentage,
"calcium_phosphorous_ratio": calciumPhosphorousRatio,
"name": name,
"omega_ratio": omegaRatio,
"omega_ratio_color": omegaRatioColor,
"calcium_phosphorous_ratio_color": calciumPhosphorousRatioColor,
};
}
class NutrientValue {
NutrientValue({
this.calcium,
this.choline,
this.copper,
this.crudeFat,
this.folate,
this.iodine,
this.iron,
this.magnesium,
this.manganese,
this.niacinB3,
this.omega3ExclAlaAndSda,
this.omega6,
this.pantothenicAcidB5,
this.phosphorus,
this.potassium,
this.protein,
this.riboflavinB2,
this.selenium,
this.sodiumNa,
this.thiaminB1,
this.vitaminA,
this.vitaminC,
this.vitaminD,
this.vitaminE,
this.zincZn,
this.calories,
});
List<dynamic> calcium;
List<dynamic> choline;
List<dynamic> copper;
List<dynamic> crudeFat;
List<dynamic> folate;
List<dynamic> iodine;
List<dynamic> iron;
List<dynamic> magnesium;
List<dynamic> manganese;
List<dynamic> niacinB3;
List<dynamic> omega3ExclAlaAndSda;
List<dynamic> omega6;
List<dynamic> pantothenicAcidB5;
List<dynamic> phosphorus;
List<dynamic> potassium;
List<dynamic> protein;
List<dynamic> riboflavinB2;
List<dynamic> selenium;
List<dynamic> sodiumNa;
List<dynamic> thiaminB1;
List<dynamic> vitaminA;
List<dynamic> vitaminC;
List<dynamic> vitaminD;
List<dynamic> vitaminE;
List<dynamic> zincZn;
List<dynamic> calories;
factory NutrientValue.fromJson(Map<String, dynamic> json) => NutrientValue(
calcium: json["Calcium"] == null
? null
: List<dynamic>.from(json["Calcium"].map((x) => x)),
choline: json["Choline"] == null
? null
: List<dynamic>.from(json["Choline"].map((x) => x)),
copper: json["Copper"] == null
? null
: List<dynamic>.from(json["Copper"].map((x) => x)),
crudeFat: json["Crude fat"] == null
? null
: List<dynamic>.from(json["Crude fat"].map((x) => x)),
folate: json["Folate"] == null
? null
: List<dynamic>.from(json["Folate"].map((x) => x)),
iodine: json["Iodine"] == null
? null
: List<dynamic>.from(json["Iodine"].map((x) => x)),
iron: json["Iron"] == null
? null
: List<dynamic>.from(json["Iron"].map((x) => x)),
magnesium: json["Magnesium"] == null
? null
: List<dynamic>.from(json["Magnesium"].map((x) => x)),
manganese: json["Manganese"] == null
? null
: List<dynamic>.from(json["Manganese"].map((x) => x)),
niacinB3: json["Niacin (B3)"] == null
? null
: List<dynamic>.from(json["Niacin (B3)"].map((x) => x)),
omega3ExclAlaAndSda: json["Omega-3 excl. ALA and SDA"] == null
? null
: List<dynamic>.from(
json["Omega-3 excl. ALA and SDA"].map((x) => x)),
omega6: json["Omega-6"] == null
? null
: List<dynamic>.from(json["Omega-6"].map((x) => x)),
pantothenicAcidB5: json["Pantothenic acid (B5)"] == null
? null
: List<dynamic>.from(json["Pantothenic acid (B5)"].map((x) => x)),
phosphorus: json["Phosphorus"] == null
? null
: List<dynamic>.from(json["Phosphorus"].map((x) => x)),
potassium: json["Potassium"] == null
? null
: List<dynamic>.from(json["Potassium"].map((x) => x)),
protein: json["Protein"] == null
? null
: List<dynamic>.from(json["Protein"].map((x) => x)),
riboflavinB2: json["Riboflavin (B2)"] == null
? null
: List<dynamic>.from(json["Riboflavin (B2)"].map((x) => x)),
selenium: json["Selenium"] == null
? null
: List<dynamic>.from(json["Selenium"].map((x) => x)),
sodiumNa: json["Sodium (Na)"] == null
? null
: List<dynamic>.from(json["Sodium (Na)"].map((x) => x)),
thiaminB1: json["Thiamin (B1)"] == null
? null
: List<dynamic>.from(json["Thiamin (B1)"].map((x) => x)),
vitaminA: json["Vitamin A"] == null
? null
: List<dynamic>.from(json["Vitamin A"].map((x) => x)),
vitaminC: json["Vitamin C"] == null
? null
: List<dynamic>.from(json["Vitamin C"].map((x) => x)),
vitaminD: json["Vitamin D"] == null
? null
: List<dynamic>.from(json["Vitamin D"].map((x) => x)),
vitaminE: json["Vitamin E"] == null
? null
: List<dynamic>.from(json["Vitamin E"].map((x) => x)),
zincZn: json["Zinc (Zn)"] == null
? null
: List<dynamic>.from(json["Zinc (Zn)"].map((x) => x)),
calories: json["Calories"] == null
? null
: List<dynamic>.from(json["Calories"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"Calcium":
calcium == null ? null : List<dynamic>.from(calcium.map((x) => x)),
"Choline":
choline == null ? null : List<dynamic>.from(choline.map((x) => x)),
"Copper":
copper == null ? null : List<dynamic>.from(copper.map((x) => x)),
"Crude fat": crudeFat == null
? null
: List<dynamic>.from(crudeFat.map((x) => x)),
"Folate":
folate == null ? null : List<dynamic>.from(folate.map((x) => x)),
"Iodine":
iodine == null ? null : List<dynamic>.from(iodine.map((x) => x)),
"Iron": iron == null ? null : List<dynamic>.from(iron.map((x) => x)),
"Magnesium": magnesium == null
? null
: List<dynamic>.from(magnesium.map((x) => x)),
"Manganese": manganese == null
? null
: List<dynamic>.from(manganese.map((x) => x)),
"Niacin (B3)": niacinB3 == null
? null
: List<dynamic>.from(niacinB3.map((x) => x)),
"Omega-3 excl. ALA and SDA": omega3ExclAlaAndSda == null
? null
: List<dynamic>.from(omega3ExclAlaAndSda.map((x) => x)),
"Omega-6":
omega6 == null ? null : List<dynamic>.from(omega6.map((x) => x)),
"Pantothenic acid (B5)": pantothenicAcidB5 == null
? null
: List<dynamic>.from(pantothenicAcidB5.map((x) => x)),
"Phosphorus": phosphorus == null
? null
: List<dynamic>.from(phosphorus.map((x) => x)),
"Potassium": potassium == null
? null
: List<dynamic>.from(potassium.map((x) => x)),
"Protein":
protein == null ? null : List<dynamic>.from(protein.map((x) => x)),
"Riboflavin (B2)": riboflavinB2 == null
? null
: List<dynamic>.from(riboflavinB2.map((x) => x)),
"Selenium": selenium == null
? null
: List<dynamic>.from(selenium.map((x) => x)),
"Sodium (Na)": sodiumNa == null
? null
: List<dynamic>.from(sodiumNa.map((x) => x)),
"Thiamin (B1)": thiaminB1 == null
? null
: List<dynamic>.from(thiaminB1.map((x) => x)),
"Vitamin A": vitaminA == null
? null
: List<dynamic>.from(vitaminA.map((x) => x)),
"Vitamin C": vitaminC == null
? null
: List<dynamic>.from(vitaminC.map((x) => x)),
"Vitamin D": vitaminD == null
? null
: List<dynamic>.from(vitaminD.map((x) => x)),
"Vitamin E": vitaminE == null
? null
: List<dynamic>.from(vitaminE.map((x) => x)),
"Zinc (Zn)":
zincZn == null ? null : List<dynamic>.from(zincZn.map((x) => x)),
"Calories": calories == null
? null
: List<dynamic>.from(calories.map((x) => x)),
};
}
class Suggestion {
Suggestion({
this.addFood,
this.removeFood,
});
AddFood addFood;
RemoveFood removeFood;
factory Suggestion.fromJson(Map<String, dynamic> json) => Suggestion(
addFood: AddFood.fromJson(json["add_food"]),
removeFood: RemoveFood.fromJson(json["remove_food"]),
);
Map<String, dynamic> toJson() => {
"add_food": addFood.toJson(),
"remove_food": removeFood.toJson(),
};
}
class AddFood {
AddFood({
this.yellow,
this.red,
});
List<Yellow> yellow;
List<Map<String, List<String>>> red;
factory AddFood.fromJson(Map<String, dynamic> json) => AddFood(
yellow:
List<Yellow>.from(json["Yellow"].map((x) => Yellow.fromJson(x))),
red: List<Map<String, List<String>>>.from(json["Red"].map((x) =>
Map.from(x).map((k, v) => MapEntry<String, List<String>>(
k, List<String>.from(v.map((x) => x)))))),
);
Map<String, dynamic> toJson() => {
"Yellow": List<dynamic>.from(yellow.map((x) => x.toJson())),
"Red": List<dynamic>.from(red.map((x) => Map.from(x).map((k, v) =>
MapEntry<String, dynamic>(
k, List<dynamic>.from(v.map((x) => x)))))),
};
}
class Yellow {
Yellow({
this.calcium,
this.phosphorus,
});
List<String> calcium;
List<String> phosphorus;
factory Yellow.fromJson(Map<String, dynamic> json) => Yellow(
calcium: json["Calcium"] == null
? null
: List<String>.from(json["Calcium"].map((x) => x)),
phosphorus: json["Phosphorus"] == null
? null
: List<String>.from(json["Phosphorus"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"Calcium":
calcium == null ? null : List<dynamic>.from(calcium.map((x) => x)),
"Phosphorus": phosphorus == null
? null
: List<dynamic>.from(phosphorus.map((x) => x)),
};
}
class RemoveFood {
RemoveFood({
this.yellow,
this.red,
});
List<dynamic> yellow;
List<Red> red;
factory RemoveFood.fromJson(Map<String, dynamic> json) => RemoveFood(
yellow: List<dynamic>.from(json["Yellow"].map((x) => x)),
red: List<Red>.from(json["Red"].map((x) => Red.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"Yellow": List<dynamic>.from(yellow.map((x) => x)),
"Red": List<dynamic>.from(red.map((x) => x.toJson())),
};
}
class Red {
Red({
this.magnesium,
this.potassium,
this.selenium,
});
List<String> magnesium;
List<String> potassium;
List<String> selenium;
factory Red.fromJson(Map<String, dynamic> json) => Red(
magnesium: json["Magnesium"] == null
? null
: List<String>.from(json["Magnesium"].map((x) => x)),
potassium: json["Potassium"] == null
? null
: List<String>.from(json["Potassium"].map((x) => x)),
selenium: json["Selenium"] == null
? null
: List<String>.from(json["Selenium"].map((x) => x)),
);
Map<String, dynamic> toJson() => {
"Magnesium": magnesium == null
? null
: List<dynamic>.from(magnesium.map((x) => x)),
"Potassium": potassium == null
? null
: List<dynamic>.from(potassium.map((x) => x)),
"Selenium": selenium == null
? null
: List<dynamic>.from(selenium.map((x) => x)),
};
}
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> {
Function _future;
Future<List<AllMealPlanModel>> getAllMealPlanData(String item) async {
String jsonString = '''
[
{
"id": 7,
"daily_allowance_actual_percentage": {
"Bone": [
0.01,
"Red"
],
"Muscle Meat": [
46.15,
"Red"
],
"Fruit and Veg": [
53.84,
"Red"
]
},
"daily_calories_from_this_meal_plan": 823.25,
"imperial_weight_of_one_meal": 5.732025,
"meal_ingredients_would_make": 8,
"meals_per_day": 2,
"nutrient_value": [
{
"Calcium": [
249.5,
"mg",
"Yellow",
0.008800863,
"ounce"
]
},
{
"Choline": [
203.65,
"mg",
"Green",
0.0071835501,
"ounce"
]
},
{
"Copper": [
1.33,
"mg",
"Green",
0.00004691442,
"ounce"
]
},
{
"Crude fat": [
29.75,
"g",
"Green",
1.0494015,
"ounce"
]
},
{
"Folate": [
469,
"mcg",
"Green",
0.000016543506,
"ounce"
]
},
{
"Iodine": [
2.1,
"mcg",
"Red",
7.40754e-8,
"ounce"
]
},
{
"Iron": [
11.77,
"mg",
"Green",
0.00041517497999999997,
"ounce"
]
},
{
"Magnesium": [
253,
"mg",
"Red",
0.008924322,
"ounce"
]
},
{
"Manganese": [
1.82,
"mg",
"Green",
0.00006419868,
"ounce"
]
},
{
"Niacin (B3)": [
11.88,
"mg",
"Green",
0.00041905512,
"ounce"
]
},
{
"Omega-3 excl. ALA and SDA": [
0.03,
"g",
"Green",
0.00105822,
"ounce"
]
},
{
"Omega-6": [
0.93,
"g",
"Green",
0.03280482,
"ounce"
]
},
{
"Pantothenic acid (B5)": [
1.7,
"mg",
"Green",
0.000059965799999999994,
"ounce"
]
},
{
"Phosphorus": [
800,
"mg",
"Yellow",
0.0282192,
"ounce"
]
},
{
"Potassium": [
2766.5,
"mg",
"Red",
0.09758552100000001,
"ounce"
]
},
{
"Protein": [
56.02,
"g",
"Green",
1.9760494800000001,
"ounce"
]
},
{
"Riboflavin (B2)": [
0.54,
"mg",
"Green",
0.00001904796,
"ounce"
]
},
{
"Selenium": [
39.05,
"mcg",
"Red",
0.0000013774497,
"ounce"
]
},
{
"Sodium (Na)": [
123,
"mg",
"Green",
0.004338702,
"ounce"
]
},
{
"Thiamin (B1)": [
0.78,
"mg",
"Green",
0.00002751372,
"ounce"
]
},
{
"Vitamin A": [
4.2,
"mcg",
"Red",
1.481508e-7,
"ounce"
]
},
{
"Vitamin C": [
1.75,
"mg",
"Green",
0.0000617295,
"ounce"
]
},
{
"Vitamin D": [
0.15,
"mcg",
"Red",
5.2911000000000005e-9,
"ounce"
]
},
{
"Vitamin E": [
1.03,
"mg",
"Red",
0.000036332220000000006,
"ounce"
]
},
{
"Zinc (Zn)": [
8.24,
"mg",
"Green",
0.00029065776000000005,
"ounce"
]
},
{
"Calories": [
823.25,
"cal",
null,
"N/A",
"ounce"
]
}
],
"pet_name": "grover",
"show_weight_of_balanced_meal": false,
"suggestion": {
"add_food": {
"Yellow": [
{
"Calcium": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Phosphorus": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
}
],
"Red": [
{
"Iodine": [
"Amaranth, grain, whole, uncooked",
"Apricot, dried"
]
},
{
"Vitamin A": [
"Bean, edamame, from frozen, cooked",
"Beef Brain"
]
},
{
"Vitamin D": [
"Beef kidney, raw",
"Beef liver, raw"
]
},
{
"Vitamin E": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Choline": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Copper": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Crude fat": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Folate": [
"Amaranth, grain, whole, uncooked",
"Apple, fuji, unpeeled, raw"
]
},
{
"Iron": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Manganese": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Niacin (B3)": [
"Apple, fuji, unpeeled, raw",
"Apple, golden delicious, unpeeled, raw"
]
},
{
"Omega-3 excl. ALA and SDA": [
"Bass, fillet, raw",
"Beef Brain"
]
},
{
"Omega-6": [
"Amaranth, grain, whole, uncooked",
"Avocado, raw"
]
},
{
"Pantothenic acid (B5)": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Protein": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Riboflavin (B2)": [
"Amaranth, grain, whole, uncooked",
"Apple, fuji, unpeeled, raw"
]
},
{
"Sodium (Na)": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
},
{
"Thiamin (B1)": [
"Amaranth, grain, whole, uncooked",
"Apple, fuji, unpeeled, raw"
]
},
{
"Zinc (Zn)": [
"Amaranth, grain, whole, uncooked",
"Apple, dried"
]
}
]
},
"remove_food": {
"Yellow": [],
"Red": [
{
"Magnesium": [
"Beef, all cuts, untrimmed, raw, no bone",
"Apricot, dried"
]
},
{
"Potassium": [
"Beef, all cuts, untrimmed, raw, no bone",
"Apricot, dried"
]
},
{
"Selenium": [
"Beef, all cuts, untrimmed, raw, no bone",
"Apricot, dried"
]
}
]
}
},
"total_calories_in_a_meal_plan": 3293,
"total_imperial_weight_of_a_meal_plan": 45.8562,
"total_weight_of_a_meal_plan": 1300,
"weight_of_one_meal": 162.5,
"feeding_plan_details": [
{
"id": 23,
"imperial_quantity": 14.1096,
"quantity": 400,
"food": {
"id": 5,
"bone_percentage": 0,
"calcium_phosphorous_ratio": 0.14,
"name": "Beef, all cuts, untrimmed, raw, no bone",
"omega_ratio": 0,
"omega_ratio_color": "Red",
"calcium_phosphorous_ratio_color": "Red"
}
},
{
"id": 24,
"imperial_quantity": 7.0548,
"quantity": 200,
"food": {
"id": 45,
"bone_percentage": 0,
"calcium_phosphorous_ratio": 0.56,
"name": "Apricot, dried",
"omega_ratio": 0,
"omega_ratio_color": "Red",
"calcium_phosphorous_ratio_color": "Yellow"
}
},
{
"id": 25,
"imperial_quantity": 17.637,
"quantity": 500,
"food": {
"id": 222,
"bone_percentage": 0,
"calcium_phosphorous_ratio": 0.38,
"name": "Bean, haricot, dried",
"omega_ratio": 0,
"omega_ratio_color": "Red",
"calcium_phosphorous_ratio_color": "Red"
}
},
{
"id": 26,
"imperial_quantity": 7.0548,
"quantity": 200,
"food": {
"id": 294,
"bone_percentage": 0,
"calcium_phosphorous_ratio": 0.08,
"name": "Pork, all cuts, lean, raw",
"omega_ratio": 0.09,
"omega_ratio_color": "Yellow",
"calcium_phosphorous_ratio_color": "Red"
}
}
]
}
]
''';
http.Response response = http.Response(jsonString, 200);
if (response.statusCode == 200) {
return allMealPlanModelFromJson(response.body);
}
}
@override
void initState() {
_future = getAllMealPlanData;
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder(
future: _future("your item"),
builder: (context, AsyncSnapshot<List<AllMealPlanModel>> 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.separated(
separatorBuilder: (BuildContext context, int index) {
return SizedBox(
height: 10,
);
},
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(snapshot.data[index].id.toString()),
Container(
//height: 50,
child: ListView.separated(
separatorBuilder:
(BuildContext context,
int index) {
return SizedBox(
width: 10,
);
},
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: snapshot.data[index]
.feedingPlanDetails.length,
itemBuilder: (context, index1) {
return Row(
children: <Widget>[
Text(
"food: ${snapshot.data[index].feedingPlanDetails[index1].food.name}"),
SizedBox(
width: 10,
),
Text(
"quantity: ${snapshot.data[index].feedingPlanDetails[index1].quantity.toString()}"),
],
);
}),
)
])
],
);
});
}
}
}));
}
}
关于json - 将Json响应中的对象映射到Flutter中的List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64583151/
我正在尝试从flutter应用程序构建apk,但出现此错误: Note: /mnt/Software/Linux/Flutter/flutter/.pub-cache/hosted/pub.dartl
我有一个名为 X 的较大应用程序,还有另一个名为 Y 的较小应用程序。他们现在彼此分开,并且工作正常。我想将应用程序 Y 集成到 X 中。我想将 Y 的代码放入 X 项目中,但它们应该有不同的 Mai
在android Studio中选择Create New Flutter Project,出现如下4个选项。 Flutter 应用程序 Flutter 插件 Flutter 包 flutter 模块
我看到我的 flutter 项目生成了一个文件 ios/Flutter/Flutter.podspec ,这个文件有什么用? 如果它与生成的 Flutter.framework 有关? 我应该将它包含
我尝试过的 在包含flutter SDK的位置添加/编辑.bash_profile.rtf文件。 导出PATH = / Users / temur / Documents / Projects / F
Flutter 日志会打印数千个详细/垃圾邮件日志。 我正在尝试调试一个复杂的应用程序,但是打印太冗长了,以至于我很难找到我自己打印的东西。 有没有办法禁用详细? 就像是: Logger.level.
在flutter 1.22更新之后,我在Lineargradient colors属性中遇到错误,这给我一个错误,即未定义名称colors参数。.在Android中更新flutter和flutter插
在下面的代码 widget.hintText 中给出错误,我试图将日期选择器作为单独的组件,并在从其他文件调用它时动态传递提示文本值。 import 'package:date_field/date_
在下面的代码 widget.hintText 中给出错误,我试图将日期选择器作为单独的组件,并在从其他文件调用它时动态传递提示文本值。 import 'package:date_field/date_
Flutter 1.0 发布后,我正在按照步骤搭建 Flutter 开发环境。 在步骤中(如所附屏幕截图所示),它说要更新 $PATH 两次,一次使用 flutter 工具的路径 export PAT
我有一个用 flutter 编写的移动应用程序,我想将其转换为 flutter_web 应用程序(集成 flutter_web 尚不可用)。我目前遇到包裹问题。 我已按照本网站中列出的说明进行操作 h
如何向我的 Flutter 路由添加自定义转换?这是我目前的路线结构。 class MyApp extends StatelessWidget { // This widget is the
我正在尝试通过 URL 在 webview 中显示网页。我试过 flutter_webview_plugin 插件,但是当我在浏览器上运行项目时它不起作用。 在 flutter web applica
我正在使用 animatedContainer 在按下按钮时显示 listView.builder()。这是一个要显示的简单子(monad)列表,但问题是我不知道 ListView 构建器的高度会传递
我目前正在我的应用程序中制作渐变背景动画......我正在使用 lottie 动画的帮助下这样做!我试图将它封装在一个容器中并成功地做到了。但是有一个问题,尽管我将高度更改为大于 2000 的东西,但
美好的一天! 我无法弄清楚如何使用 google 标签管理器设置 flutter。我找到了 this package包括标签管理器 api。但是我不知道如何正确配置它。 (在网络上我只需要复制粘贴一个
我的购物车模型如下 class Cart { String description; double unitCost; double amount; int quantity; S
在 Flutter 应用程序中,我想为在线托管的资源(图像、视频等)实现缓存。 我希望它能在原生平台 (Android/iOS)(例如使用文件系统)和网络(例如使用 IndexedDB)上运行。 Fl
我写了一个页面,在顶部一切都很好,应该是这样。在底部我有一个事件的历史。我的容器的宽度是自动确定的(取决于屏幕的宽度),而高度 - 不,在不同的设备上有不同的高度(底部的缩进是不同的)。是否可以自动确
我正在处理一个页面,其中有一些字段,例如 textfield 和 slider。在页面的末尾必须有一个用于进行下一步的按钮,该按钮被包裹在 Align 中以在页面之间具有固定位置。 另一方面,resi
我是一名优秀的程序员,十分优秀!