从 api 错误中获取 Json 未处理的异常:类型 '_InternalLinkedHashMap<String, dynamic>' 不是类型 'List<dynamic>' 的子类型
Fetching Json from api error Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'
你好,我有一个问题,我正在从 api 中获取类别,这在邮递员中工作正常但在 flutter 中我没有使用模型 class 只是使用 dio 我需要获取这个 json api 作为类别顶部栏垂直滚动的数组。
这是我的邮递员回复
{
"data": [
{
"id": 1,
"name": "Electronics",
"color": "#ff8000"
},
{
"id": 2,
"name": "SpareParts",
"color": "#00ff40"
},
]
}
我用了 flutter
getCatagories() async {
var tokens;
var dioo = Dio();
dioo.interceptors
.add(InterceptorsWrapper(onRequest: (RequestOptions options) async {
var customHeaders = {
'content-type': 'application/json',
};
options.headers.addAll(customHeaders);
return options;
}));
var response = await dioo.get("https://www.mywebiste.com/api/cat");
if(response.data.isEmpty||response.data==null)
{
}
else
{
}
return response.data;
}
获取这些类别
我用过
List categories = [];
@override
void initState() {
getCatagories().then((data) {
setState(() {
categories = data;
});
});
super.initState();
}
// by default first item will be selected
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(vertical: kDefaultPadding / 2),
height: 30,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categories.length,
itemBuilder: (context, index) => GestureDetector(
onTap: () {
Fluttertoast.showToast(
msg: "This is Center Short Toast"+selectedIndex.toString(),
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
setState(() {
selectedIndex = index;
});
},
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.only(
left: kDefaultPadding,
// At end item it add extra 20 right padding
right: index == categories.length - 1 ? kDefaultPadding : 0,
),
padding: EdgeInsets.symmetric(horizontal: kDefaultPadding),
decoration: BoxDecoration(
color: index == selectedIndex
? Colors.white.withOpacity(0.4)
: Colors.transparent,
borderRadius: BorderRadius.circular(6),
),
child: Text(
categories[index],
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
所以当我 运行 这些应用程序类别不会出现在滚动条上并且错误是未处理的异常:InternalLinkedHashMap' is not a subtype of type 'List
您正在以 Map 的形式从请求中获取数据并将其分配给 List。
您应该将类别设为 Map:
Map categories;
注意:不要给它泛型。
之后,在类别中循环时,您将在下面的代码中遇到一些问题。
那是另一个问题。 :)
你好,我有一个问题,我正在从 api 中获取类别,这在邮递员中工作正常但在 flutter 中我没有使用模型 class 只是使用 dio 我需要获取这个 json api 作为类别顶部栏垂直滚动的数组。
这是我的邮递员回复
{
"data": [
{
"id": 1,
"name": "Electronics",
"color": "#ff8000"
},
{
"id": 2,
"name": "SpareParts",
"color": "#00ff40"
},
]
}
我用了 flutter
getCatagories() async {
var tokens;
var dioo = Dio();
dioo.interceptors
.add(InterceptorsWrapper(onRequest: (RequestOptions options) async {
var customHeaders = {
'content-type': 'application/json',
};
options.headers.addAll(customHeaders);
return options;
}));
var response = await dioo.get("https://www.mywebiste.com/api/cat");
if(response.data.isEmpty||response.data==null)
{
}
else
{
}
return response.data;
}
获取这些类别 我用过
List categories = [];
@override
void initState() {
getCatagories().then((data) {
setState(() {
categories = data;
});
});
super.initState();
}
// by default first item will be selected
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(vertical: kDefaultPadding / 2),
height: 30,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categories.length,
itemBuilder: (context, index) => GestureDetector(
onTap: () {
Fluttertoast.showToast(
msg: "This is Center Short Toast"+selectedIndex.toString(),
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
setState(() {
selectedIndex = index;
});
},
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.only(
left: kDefaultPadding,
// At end item it add extra 20 right padding
right: index == categories.length - 1 ? kDefaultPadding : 0,
),
padding: EdgeInsets.symmetric(horizontal: kDefaultPadding),
decoration: BoxDecoration(
color: index == selectedIndex
? Colors.white.withOpacity(0.4)
: Colors.transparent,
borderRadius: BorderRadius.circular(6),
),
child: Text(
categories[index],
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
所以当我 运行 这些应用程序类别不会出现在滚动条上并且错误是未处理的异常:InternalLinkedHashMap
您正在以 Map 的形式从请求中获取数据并将其分配给 List。 您应该将类别设为 Map:
Map categories;
注意:不要给它泛型。
之后,在类别中循环时,您将在下面的代码中遇到一些问题。 那是另一个问题。 :)