我很难在后台解析JSON。我在使用FoodData API。
下面是一个菜肴的示例响应:
{
"fdcId": 167782,
"description": "Abiyuch, raw",
"dataType": "SR Legacy",
"publicationDate": "2019-04-01",
"ndbNumber": "9427",
"foodNutrients": [
{
"number": "318",
"name": "Vitamin A, IU",
"amount": 100,
"unitName": "IU",
"derivationCode": "A",
"derivationDescription": "Analytical"
},
{
"number": "268",
"name": "Energy",
"amount": 290,
"unitName": "kJ",
"derivationCode": "NC",
"derivationDescription": "Calculated"
},
]
class FoodGen {
int fdcId;
String description;
String dataType;
String publicationDate;
String ndbNumber;
List<FoodNutrients> foodNutrients;
FoodGen(
{this.fdcId,
this.description,
this.dataType,
this.publicationDate,
this.ndbNumber,
this.foodNutrients});
FoodGen.fromJson(Map<String, dynamic> json) {
fdcId = json['fdcId'];
description = json['description'];
dataType = json['dataType'];
publicationDate = json['publicationDate'];
ndbNumber = json['ndbNumber'];
if (json['foodNutrients'] != null) {
foodNutrients = new List<FoodNutrients>();
json['foodNutrients'].forEach((v) {
foodNutrients.add(new FoodNutrients.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['fdcId'] = this.fdcId;
data['description'] = this.description;
data['dataType'] = this.dataType;
data['publicationDate'] = this.publicationDate;
data['ndbNumber'] = this.ndbNumber;
if (this.foodNutrients != null) {
data['foodNutrients'] =
this.foodNutrients.map((v) => v.toJson()).toList();
}
return data;
}
}
class FoodNutrients {
String number;
String name;
double amount;
String unitName;
String derivationCode;
String derivationDescription;
FoodNutrients(
{this.number,
this.name,
this.amount,
this.unitName,
this.derivationCode,
this.derivationDescription});
FoodNutrients.fromJson(Map<String, dynamic> json) {
number = json['number'];
name = json['name'];
amount = json['amount'];
unitName = json['unitName'];
derivationCode = json['derivationCode'];
derivationDescription = json['derivationDescription'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['number'] = this.number;
data['name'] = this.name;
data['amount'] = this.amount;
data['unitName'] = this.unitName;
data['derivationCode'] = this.derivationCode;
data['derivationDescription'] = this.derivationDescription;
return data;
}
}
import 'dart:async';
import 'dart:convert';
import 'package:fit_app/fitness_app_theme.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<FoodGen>> fetchPhotos(http.Client client) async {
final response =
await client.get('https://api.nal.usda.gov/fdc/v1/foods/list?dataType=Foundation,SR%20Legacy&pageSize=25&api_key=h8GO51P1H4e0dfvWOmVsu75dafKwNqJk41kf0HMD');
// Use the compute function to run parsePhotos in a separate isolate.
return compute(parsePhotos, response.body);
}
// A function that converts a response body into a List<Photo>.
List<FoodGen> parsePhotos(String responseBody) {
final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<FoodGen>((json) => FoodGen.fromJson(json)).toList();
}
// To parse this JSON data, do
//
// final welcome = welcomeFromJson(jsonString);
class FoodGen {
int fdcId;
String description;
String dataType;
String publicationDate;
String ndbNumber;
List<FoodNutrients> foodNutrients;
FoodGen(
{this.fdcId,
this.description,
this.dataType,
this.publicationDate,
this.ndbNumber,
this.foodNutrients});
FoodGen.fromJson(Map<String, dynamic> json) {
fdcId = json['fdcId'];
description = json['description'];
dataType = json['dataType'];
publicationDate = json['publicationDate'];
ndbNumber = json['ndbNumber'];
if (json['foodNutrients'] != null) {
foodNutrients = new List<FoodNutrients>();
json['foodNutrients'].forEach((v) {
foodNutrients.add(new FoodNutrients.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['fdcId'] = this.fdcId;
data['description'] = this.description;
data['dataType'] = this.dataType;
data['publicationDate'] = this.publicationDate;
data['ndbNumber'] = this.ndbNumber;
if (this.foodNutrients != null) {
data['foodNutrients'] =
this.foodNutrients.map((v) => v.toJson()).toList();
}
return data;
}
}
class FoodNutrients {
String number;
String name;
double amount;
String unitName;
String derivationCode;
String derivationDescription;
FoodNutrients(
{this.number,
this.name,
this.amount,
this.unitName,
this.derivationCode,
this.derivationDescription});
FoodNutrients.fromJson(Map<String, dynamic> json) {
number = json['number'];
name = json['name'];
amount = json['amount'];
unitName = json['unitName'];
derivationCode = json['derivationCode'];
derivationDescription = json['derivationDescription'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['number'] = this.number;
data['name'] = this.name;
data['amount'] = this.amount;
data['unitName'] = this.unitName;
data['derivationCode'] = this.derivationCode;
data['derivationDescription'] = this.derivationDescription;
return data;
}
}
class FoodPage extends StatefulWidget {
FoodPage({Key key}) : super(key: key);
@override
_FoodPageState createState() => _FoodPageState();
}
class _FoodPageState extends State<FoodPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: FitnessAppTheme.darkBackground,
body: FutureBuilder<List<FoodGen>>(
future: fetchPhotos(http.Client()),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? PhotosList(photos: snapshot.data)
: Center(child: CircularProgressIndicator());
},
),
);
}
}
class PhotosList extends StatelessWidget {
final List<FoodGen> photos;
PhotosList({Key key, this.photos}) : super(key: key);
@override
Widget build(BuildContext context) {
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: photos.length,
itemBuilder: (context, index) {
Text(photos[index].description.toString());
},
);
}
}
您需要将双倍量;
替换为零倍量;
我得到一个错误:输入'List'
我试图在共享首选项中设置字符串列表,但出于未知原因,它会抛出以下错误 在类型转换中,类型“bool”不是类型“List”的子类型 这是我的密码 我没有将bool类型值分配给任何变量,那么为什么会出现此错误?
上面的代码给出了警告未处理的异常:类型'double'不是类型'string'的子类型
我无法理解这个问题。我将下面的文档设置为firesta。我需要将出勤字段转换为列表。 使用StreamProvider。这条小溪没有问题。使用toList() 文件被取出。但模型正在失败。获取异常:[类型“\u InternalLinkedHashMap”不是类型“Map”的子类型]模型类: 哪里出错了。我需要一点帮助。非常感谢。
我是新手。我正在开发一个测验应用程序,并拥有以下三个dart文件: 主要的飞奔 question.dart answer.dart 当我在android studio中的android上运行应用程序时,出现以下错误: ══╡ 小部件库捕获的异常╞═══════════════════════════════════════════ 生成MyApp时引发了以下类型的错误(脏,状态:_MyAppSta
在存储到本地数据库之前,将字符串转换为对象的正确方法是什么? 这是的输出: 我试图将其转换为CreatedBy对象 创造的 这里是我的本地表列 错误