Flutter 颤振Json未处理异常:类型'_内部链接dhashmap<;字符串,动态>';不是类型为';Iterable<;动态>';
类别 错误:未处理的异常:类型“\u InternalLinkedHashMap”不是类型“Iterable”的子类型Flutter 颤振Json未处理异常:类型'_内部链接dhashmap<;字符串,动态>';不是类型为';Iterable<;动态>';,flutter,Flutter,类别 错误:未处理的异常:类型“\u InternalLinkedHashMap”不是类型“Iterable”的子类型 class Category { int id; String categoryName; String seoUrl; Category(this.id,this.categoryName,this.seoUrl); Category.fromJson(Map json){ id = json["id"]; c
class Category {
int id;
String categoryName;
String seoUrl;
Category(this.id,this.categoryName,this.seoUrl);
Category.fromJson(Map json){
id = json["id"];
categoryName = json["categoryName"];
seoUrl = json["seoUrl"];
}
Map toJson(){
return {
"id": id,
"categoryName": categoryName,
"seoUrl": seoUrl
};
}
}
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http_demo/data/api/category_api.dart';
import 'package:http_demo/models/category.dart';
class MainScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MainScreenState();
}
}
class MainScreenState extends State {
List<Category> categories = List<Category>();
List<Widget> categoryWidgets = List<Widget>();
@override
void initState() {
getCategoriesFromApi();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Alışveriş Sistemi",
style: TextStyle(color: Colors.white),
),
backgroundColor: Colors.blueGrey,
centerTitle: true,
),
body: Padding(
padding: EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: categoryWidgets,
),
)
],
),
),
);
}
void getCategoriesFromApi() {
CategoryApi.getCategories().then((response) {
setState(() {
Iterable list = json.decode(response.body);
//categories = list.map((category) => Category.fromJson(category)).toList();
this.categories = List<Category>.from(list.map((model)=> Category.fromJson(model)));
getCategoryWidgets();
});
});
}
List<Widget> getCategoryWidgets() {
for (int i = 0; i < categories.length; i++) {
categoryWidgets.add(getCategoryWidget(categories[i]));
}
return categoryWidgets;
}
Widget getCategoryWidget(Category category) {
return FlatButton(
onPressed: null,
child: Text(
category.categoryName,
style: TextStyle(color: Colors.blueGrey),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
side: BorderSide(color: Colors.lightGreenAccent),
),
);
}
}
类主屏幕
错误:未处理的异常:类型“\u InternalLinkedHashMap”不是类型“Iterable”的子类型
class Category {
int id;
String categoryName;
String seoUrl;
Category(this.id,this.categoryName,this.seoUrl);
Category.fromJson(Map json){
id = json["id"];
categoryName = json["categoryName"];
seoUrl = json["seoUrl"];
}
Map toJson(){
return {
"id": id,
"categoryName": categoryName,
"seoUrl": seoUrl
};
}
}
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http_demo/data/api/category_api.dart';
import 'package:http_demo/models/category.dart';
class MainScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MainScreenState();
}
}
class MainScreenState extends State {
List<Category> categories = List<Category>();
List<Widget> categoryWidgets = List<Widget>();
@override
void initState() {
getCategoriesFromApi();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Alışveriş Sistemi",
style: TextStyle(color: Colors.white),
),
backgroundColor: Colors.blueGrey,
centerTitle: true,
),
body: Padding(
padding: EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: categoryWidgets,
),
)
],
),
),
);
}
void getCategoriesFromApi() {
CategoryApi.getCategories().then((response) {
setState(() {
Iterable list = json.decode(response.body);
//categories = list.map((category) => Category.fromJson(category)).toList();
this.categories = List<Category>.from(list.map((model)=> Category.fromJson(model)));
getCategoryWidgets();
});
});
}
List<Widget> getCategoryWidgets() {
for (int i = 0; i < categories.length; i++) {
categoryWidgets.add(getCategoryWidget(categories[i]));
}
return categoryWidgets;
}
Widget getCategoryWidget(Category category) {
return FlatButton(
onPressed: null,
child: Text(
category.categoryName,
style: TextStyle(color: Colors.blueGrey),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
side: BorderSide(color: Colors.lightGreenAccent),
),
);
}
}
导入“dart:convert”;
进口“包装:颤振/材料.省道”;
导入“包:http_demo/data/api/category_api.dart”;
导入“包:http_demo/models/category.dart”;
类MainScreen扩展StatefulWidget{
@凌驾
状态createState(){
返回MainScreenState();
}
}
类MainScreenState扩展了状态{
列表类别=列表();
List categoryWidgets=List();
@凌驾
void initState(){
getCategoriesFromApi();
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:正文(
“安全系统”,
样式:TextStyle(颜色:Colors.white),
),
背景颜色:颜色。蓝灰色,
标题:对,
),
主体:填充物(
填充:所有边缘设置(10.0),
子:列(
儿童:[
SingleChildScrollView(
滚动方向:轴水平,
孩子:排(
子类:categoryWidgets,
),
)
],
),
),
);
}
void getCategoriesFromApi(){
CategoryApi.getCategories().then((响应){
设置状态(){
Iterable list=json.decode(response.body);
//categories=list.map((category)=>category.fromJson(category)).toList();
this.categories=List.from(List.map((model)=>Category.fromJson(model));
getCategoryWidgets();
});
});
}
列表getCategoryWidgets(){
对于(int i=0;i
错误:未处理的异常:类型“\u InternalLinkedHashMap”不是类型“Iterable”的子类型。
错误:未处理的异常:类型“\u InternalLinkedHashMap”不是类型“Iterable”的子类型
错误:未处理的异常:类型“\u InternalLinkedHashMap”不是类型“Iterable”的子类型
错误:未处理的异常:类型“\u InternalLinkedHashMap”不是类型“Iterable”的子类型
错误:未处理的异常:类型“\u InternalLinkedHashMap”不是类型“Iterable”的子类型请在转换JSON后检查响应
class Category {
int id;
String categoryName;
String seoUrl;
Category(this.id,this.categoryName,this.seoUrl);
Category.fromJson(Map json){
id = json["id"];
categoryName = json["categoryName"];
seoUrl = json["seoUrl"];
}
Map toJson(){
return {
"id": id,
"categoryName": categoryName,
"seoUrl": seoUrl
};
}
}
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http_demo/data/api/category_api.dart';
import 'package:http_demo/models/category.dart';
class MainScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MainScreenState();
}
}
class MainScreenState extends State {
List<Category> categories = List<Category>();
List<Widget> categoryWidgets = List<Widget>();
@override
void initState() {
getCategoriesFromApi();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Alışveriş Sistemi",
style: TextStyle(color: Colors.white),
),
backgroundColor: Colors.blueGrey,
centerTitle: true,
),
body: Padding(
padding: EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: categoryWidgets,
),
)
],
),
),
);
}
void getCategoriesFromApi() {
CategoryApi.getCategories().then((response) {
setState(() {
Iterable list = json.decode(response.body);
//categories = list.map((category) => Category.fromJson(category)).toList();
this.categories = List<Category>.from(list.map((model)=> Category.fromJson(model)));
getCategoryWidgets();
});
});
}
List<Widget> getCategoryWidgets() {
for (int i = 0; i < categories.length; i++) {
categoryWidgets.add(getCategoryWidget(categories[i]));
}
return categoryWidgets;
}
Widget getCategoryWidget(Category category) {
return FlatButton(
onPressed: null,
child: Text(
category.categoryName,
style: TextStyle(color: Colors.blueGrey),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
side: BorderSide(color: Colors.lightGreenAccent),
),
);
}
}
void getCategoriesFromApi() {
CategoryApi.getCategories().then((response) {
setState(() {
var list = json.decode(response.body);
//categories = list.map((category) => Category.fromJson(category)).toList();
if (list is List) {
this.categories = list.map((e) => Category.fromJson(e)).toList();
getCategoryWidgets();
} else {
print('response is not list');
}
});
});
}
你能分享response.body吗?我得到了文本“response is not list”import“package:http/http.dart”作为http;类CategoryApi{static Future getCategories(){return http.get(“);}}}是。您可以检查响应。print(response.body);类Category{int id;String categoryName;String seoUrl;Category(this.id,this.categoryName,this.seoUrl);Category.fromJson(Map json){id=json[“id”];categoryName=json[“categoryName”];seoUrl=json[“seoUrl”];}映射到json(){return{“id”:id,“categoryName”:categoryName,“seoUrl”:seoUrl};}}我正在使用bluestack。