我正在构建一个颤振应用程序,我必须解析api中的一些数据,我设置了所有内容,但我收到了这个错误,我不知道为什么,我是颤振新手,任何帮助都将不胜感激。谢谢。
E/flutter ( 2725): [ERROR:flutter/shell/common/shell.cc(210)] Dart Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FoodModel',
{
"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,
}
]
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
var foodList = new List<FoodModel>();
List<FoodModel> list = new List<FoodModel>();
Future<List<FoodModel>> fetchFoodCategories() async {
var url = "https://www.sitess.com/api/json/v1/1/categories.php";
var response = await http.get(url);
if(response.statusCode == 200) {
foodList.add(json.decode(response.body));
}
return foodList;
}
@override
Widget build(BuildContext context) {
fetchFoodCategories().then((value){
list.addAll(value);
});
class FoodModel {
List<Categories> _categories;
List<Categories> get categories => _categories;
FoodModel({
List<Categories> categories}){
_categories = categories;
}
FoodModel.fromJson(dynamic json) {
if (json["categories"] != null) {
_categories = [];
json["categories"].forEach((v) {
_categories.add(Categories.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
var map = <String, dynamic>{};
if (_categories != null) {
map["categories"] = _categories.map((v) => v.toJson()).toList();
}
return map;
}
}
class Categories {
String _idCategory;
String _strCategory;
String _strCategoryThumb;
String _strCategoryDescription;
String get idCategory => _idCategory;
String get strCategory => _strCategory;
String get strCategoryThumb => _strCategoryThumb;
String get strCategoryDescription => _strCategoryDescription;
Categories({
String idCategory,
String strCategory,
String strCategoryThumb,
String strCategoryDescription}){
_idCategory = idCategory;
_strCategory = strCategory;
_strCategoryThumb = strCategoryThumb;
_strCategoryDescription = strCategoryDescription;
}
Categories.fromJson(dynamic json) {
_idCategory = json["idCategory"];
_strCategory = json["strCategory"];
_strCategoryThumb = json["strCategoryThumb"];
_strCategoryDescription = json["strCategoryDescription"];
}
Map<String, dynamic> toJson() {
var map = <String, dynamic>{};
map["idCategory"] = _idCategory;
map["strCategory"] = _strCategory;
map["strCategoryThumb"] = _strCategoryThumb;
map["strCategoryDescription"] = _strCategoryDescription;
return map;
}
}
根据您提供的上述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,
};
}
这是呈现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}')
],
),
);
})
],
)));
}
}
让我知道它是否有效
发生错误的原因是您试图在foodList中添加映射,而不是FoodModel类的对象。
您需要从模型中利用FoodModel.fromJson函数。因此,在函数finchFood类别中,而不是行:
foodList.add(json.decode(response.body));
你应使用:
foodList.add(FoodModel.fromJson(json.decode(response.body)));
json。decode(response.body)
不提供您创建的FoodModel类,您需要使用类似于var jsonResponse=json的东西。解码(response.body)
然后
var categoryList=jsonResponse['categories']
以获取类别列表
问题内容: 我之前从未遇到过此错误,所以我不确定该怎么做或意味着什么 未处理的异常类型 它在以下代码中发生: 它给了我2个选项“添加抛出声明”和“使用try / catch进行环绕”。 我该怎么办,为什么? 问题答案: 这意味着您要调用的方法已使用指令声明了从类派生的异常。当以这种方式声明一个方法时,您将被迫使用一个块来处理该异常,或者将一个相同的(对于相同的异常或超类型)语句添加到您的方法声明中
我收到了错误消息: 被精确定位的线在这里: 返回类型为
我正在解码一个响应体,我得到了错误: 我在Udemy上学习颤振教程时,正在尝试使用API。教程说要使用https://javiercbk.github.io/json_to_dart/将JSON转换为Dart。我把JSON从https://www.openbrewerydb.org/并将其转换为Dart,但我遇到的问题是,当我尝试解码API时,我得到了未处理的错误异常:“List”类型不是“Map
我对使用颤振和特定于平台的代码非常陌生,所以如果这是一个愚蠢的问题,请原谅我。我正在使用一个事件通道将数据从android端返回到Flatter。我正在返回一份清单 但是,当我试图添加它时,它给出了一个异常,“未处理的异常:类型'列表'不是类型'列表'的子类型 这是我要将贴图对象添加到的列表。 列表 这是我的添加代码。忽略print语句。 我尝试过像cast或from这样的方法,但它对我不起作用,
我正在尝试从服务器获取json数据
虽然这个问题已经被问了几次,但我没有从我看到的那些人那里得到帮助。所以就这样, 我有一个模拟json文件在我的资产文件夹: 这是: 这里是: 如您所见,我希望在页面加载后立即获取数据,并将所述数据保存在列表中,然后可以在中使用。但每次加载屏幕时,我都会出现以下错误: 我接着看了一些教程,完全忽略了与我不想要的模型相关的任何东西。我错过了什么?我需要做什么才能在一个列表中得到结果,然后在网格视图中使