Json 未处理的异常:类型'_内部链接dhashmap<;字符串,动态>;
我正在构建一个颤振应用程序,我必须解析api中的一些数据,我设置了所有内容,但我收到了这个错误,我不知道为什么,我是颤振新手,任何帮助都将不胜感激。谢谢Json 未处理的异常:类型'_内部链接dhashmap<;字符串,动态>;,json,flutter,Json,Flutter,我正在构建一个颤振应用程序,我必须解析api中的一些数据,我设置了所有内容,但我收到了这个错误,我不知道为什么,我是颤振新手,任何帮助都将不胜感激。谢谢 产生的错误 这就是我处理数据的方式 类MyApp扩展了无状态小部件{ //此小部件是应用程序的根。 var foodList=新列表(); 列表=新列表(); 未来的fetchFoodCategories()异步{ 变量url=”https://www.sitess.com/api/json/v1/1/categories.php";
- 产生的错误
- 这就是我处理数据的方式
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
var foodList=新列表();
列表=新列表();
未来的fetchFoodCategories()异步{
变量url=”https://www.sitess.com/api/json/v1/1/categories.php";
var response=wait http.get(url);
如果(response.statusCode==200){
add(json.decode(response.body));
}
返回食物列表;
}
@凌驾
小部件构建(构建上下文){
fetchFoodCategories()。然后((值){
list.addAll(值);
});
- 这是模型课
class-FoodModel{
列出所有类别;
列表获取类别=>\u类别;
食品模型({
列表类别}){
_类别=类别;
}
FoodModel.fromJson(动态json){
如果(json[“类别”]!=null){
_类别=[];
json[“类别”].forEach((v){
_categories.add(categories.fromJson(v));
});
}
}
映射到JSON(){
var-map={};
如果(_categories!=null){
map[“categories”]=“u categories.map((v)=>v.toJson()).toList();
}
返回图;
}
}
类别{
字符串idCategory;
字符串(str范畴);;
字符串_strCategoryThumb;
字符串_strCategoryDescription;
字符串get-idCategory=>\u-idCategory;
字符串get strCategory=>\u strCategory;
字符串get strCategoryThumb=>\u strCategoryThumb;
字符串get-strCategoryDescription=>\u-strCategoryDescription;
类别({
字符串idCategory,
字符串结构类别,
字符串strCategory Thumb,
字符串strCategoryDescription}){
_idCategory=idCategory;
_strCategory=strCategory;
_strCategoryThumb=strCategoryThumb;
_strCategoryDescription=strCategoryDescription;
}
Categories.fromJson(动态json){
_idCategory=json[“idCategory”];
_strCategory=json[“strCategory”];
_strCategoryThumb=json[“strCategoryThumb”];
_strCategoryScription=json[“strCategoryScription”];
}
映射到JSON(){
var-map={};
地图[“idCategory”]=\u idCategory;
映射[“strCategory”]=\u strCategory;
映射[“strCategoryThumb”]=\u strCategoryThumb;
映射[“strCategoryDescription”]=\u strCategoryDescription;
返回图;
}
}
发生错误是因为您试图在食品列表中添加一个映射,而不是类食品模型的对象
您需要从模型中使用FoodModel.fromJson函数。因此,在函数中,取foodCategories,而不是行中:
foodList.add(json.decode(response.body));
你应使用:
foodList.add(FoodModel.fromJson(json.decode(response.body)));
json.decode(response.body)
没有提供您创建的FoodModel类,您需要使用类似于var-jsonResponse=json.decode(response.body);
然后var-categoryList=jsonResponse['categories'];
要从您提供的上述json中获取类别列表,我为您创建了一个示例
这是您提供的json
{
"categories":[
{
"idCategory":"1",
"strCategory":"Beef",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/beef.png",
"strCategoryDescription":"Beef is the"
},
{
"idCategory":"2",
"strCategory":"Chicken",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/chicken.png",
"strCategoryDescription":"Chicken is "
},
{
"idCategory":"3",
"strCategory":"Dessert",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/dessert.png",
"strCategoryDescription":"Dessert is a course"
},
{
"idCategory":"4",
"strCategory":"Lamb",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/lamb.png",
"strCategoryDescription":"Lamb, hogget,"
}
]
}
这是上述json数据的模型类:
// To parse this JSON data, do
//
// final fooModel = fooModelFromJson(jsonString);
import 'dart:convert';
FooModel fooModelFromJson(String str) => FooModel.fromJson(json.decode(str));
String fooModelToJson(FooModel data) => json.encode(data.toJson());
class FooModel {
FooModel({
this.categories,
});
List<Category> categories;
factory FooModel.fromJson(Map<String, dynamic> json) => FooModel(
categories: List<Category>.from(json["categories"].map((x) => Category.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"categories": List<dynamic>.from(categories.map((x) => x.toJson())),
};
}
class Category {
Category({
this.idCategory,
this.strCategory,
this.strCategoryThumb,
this.strCategoryDescription,
});
String idCategory;
String strCategory;
String strCategoryThumb;
String strCategoryDescription;
factory Category.fromJson(Map<String, dynamic> json) => Category(
idCategory: json["idCategory"],
strCategory: json["strCategory"],
strCategoryThumb: json["strCategoryThumb"],
strCategoryDescription: json["strCategoryDescription"],
);
Map<String, dynamic> toJson() => {
"idCategory": idCategory,
"strCategory": strCategory,
"strCategoryThumb": strCategoryThumb,
"strCategoryDescription": strCategoryDescription,
};
}
//要解析此JSON数据,请执行以下操作
//
//final fooModel=fooModelFromJson(jsonString);
导入“dart:convert”;
FooModel fooModelFromJson(String str)=>FooModel.fromJson(json.decode(str));
字符串fooModelToJson(FooModel数据)=>json.encode(data.toJson());
类模型{
食品模型({
这是一个类别,
});
列出类别;
工厂FooModel.fromJson(映射json)=>FooModel(
类别:List.from(json[“categories”].map((x)=>Category.fromJson(x)),
);
映射到JSON()=>{
“categories”:List.from(categories.map((x)=>x.toJson()),
};
}
类别{
类别({
这个类别,
这个.strCategory,
这个.strCategory拇指,
此.strCategory说明,
});
字符串ID类别;
弦结构范畴;
字符串strCategoryThumb;
字符串strcategory描述;
工厂类别.fromJson(映射json)=>类别(
idCategory:json[“idCategory”],
strCategory:json[“strCategory”],
strCategoryThumb:json[“strCategoryThumb”],
strCategoryDescription:json[“strCategoryDescription”],
);
映射到JSON()=>{
“idCategory”:idCategory,
“strCategory”:strCategory,
“strCategoryThumb”:strCategoryThumb,
“strCategoryDescription”:strCategoryDescription,
};
}
这是呈现ui的列表,您可以根据需要进行更改。这是一个示例
import 'package:flutter/material.dart';
import 'package:json_parsing_example/model2.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SampleApp(),
debugShowCheckedModeBanner: false,
);
}
}
class SampleApp extends StatefulWidget {
@override
_SampleAppState createState() => _SampleAppState();
}
class _SampleAppState extends State<SampleApp> {
bool _isLoading = false;
List<Category> list = List();
fetchData() async {
setState(() {
_isLoading = true;
});
String data =
await DefaultAssetBundle.of(context).loadString("json/parse.json");
// This is the above where you get the remote data
// Like var response = await http.get('your url');
final fooModel = fooModelFromJson(data);
list = fooModel.categories;
setState(() {
_isLoading = false;
});
}
@override
void initState() {
super.initState();
fetchData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your heading'),
),
body: Container(
child: _isLoading
? Center(child: CircularProgressIndicator())
: Column(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: list.length,
itemBuilder: (context, index) {
return Card(
child: Column(
children: <Widget>[
Text('${list[index].idCategory}'),
Text('${list[index].strCategory}')
],
),
);
})
],
)));
}
}
导入“包装:颤振/材料.省道”;
导入“package:json_parsing_example/model2.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:SampleApp(),
debugShowCheckedModeBanner:false,
);
}
}
类SampleApp扩展StatefulWidget{
@凌驾
_SampleAppState createState();
}
类_SampleAppState扩展状态{
bool_isLoading=false;
List=List();
fetchData()异步{
设置状态(){
_isLoading=true;
});
字符串数据=
等待DefaultAssetBundle.of(context.loadString(“json/parse.json”);
//这是上面您获取远程数据的地方
//比如var response=wait http.get('your url');
final fooModel=fooModelFromJson(数据);
列表=fooModel.categories;
设置状态(){
_isLoading=false;
});
}
@凌驾
void initState(){
super.initState();
fetchData();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“您的标题”),
),
主体:容器(
foodList.add(FoodModel.fromJson(json.decode(response.body)));
{
"categories":[
{
"idCategory":"1",
"strCategory":"Beef",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/beef.png",
"strCategoryDescription":"Beef is the"
},
{
"idCategory":"2",
"strCategory":"Chicken",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/chicken.png",
"strCategoryDescription":"Chicken is "
},
{
"idCategory":"3",
"strCategory":"Dessert",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/dessert.png",
"strCategoryDescription":"Dessert is a course"
},
{
"idCategory":"4",
"strCategory":"Lamb",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/lamb.png",
"strCategoryDescription":"Lamb, hogget,"
}
]
}
// To parse this JSON data, do
//
// final fooModel = fooModelFromJson(jsonString);
import 'dart:convert';
FooModel fooModelFromJson(String str) => FooModel.fromJson(json.decode(str));
String fooModelToJson(FooModel data) => json.encode(data.toJson());
class FooModel {
FooModel({
this.categories,
});
List<Category> categories;
factory FooModel.fromJson(Map<String, dynamic> json) => FooModel(
categories: List<Category>.from(json["categories"].map((x) => Category.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"categories": List<dynamic>.from(categories.map((x) => x.toJson())),
};
}
class Category {
Category({
this.idCategory,
this.strCategory,
this.strCategoryThumb,
this.strCategoryDescription,
});
String idCategory;
String strCategory;
String strCategoryThumb;
String strCategoryDescription;
factory Category.fromJson(Map<String, dynamic> json) => Category(
idCategory: json["idCategory"],
strCategory: json["strCategory"],
strCategoryThumb: json["strCategoryThumb"],
strCategoryDescription: json["strCategoryDescription"],
);
Map<String, dynamic> toJson() => {
"idCategory": idCategory,
"strCategory": strCategory,
"strCategoryThumb": strCategoryThumb,
"strCategoryDescription": strCategoryDescription,
};
}
import 'package:flutter/material.dart';
import 'package:json_parsing_example/model2.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SampleApp(),
debugShowCheckedModeBanner: false,
);
}
}
class SampleApp extends StatefulWidget {
@override
_SampleAppState createState() => _SampleAppState();
}
class _SampleAppState extends State<SampleApp> {
bool _isLoading = false;
List<Category> list = List();
fetchData() async {
setState(() {
_isLoading = true;
});
String data =
await DefaultAssetBundle.of(context).loadString("json/parse.json");
// This is the above where you get the remote data
// Like var response = await http.get('your url');
final fooModel = fooModelFromJson(data);
list = fooModel.categories;
setState(() {
_isLoading = false;
});
}
@override
void initState() {
super.initState();
fetchData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your heading'),
),
body: Container(
child: _isLoading
? Center(child: CircularProgressIndicator())
: Column(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: list.length,
itemBuilder: (context, index) {
return Card(
child: Column(
children: <Widget>[
Text('${list[index].idCategory}'),
Text('${list[index].strCategory}')
],
),
);
})
],
)));
}
}