如何修复此错误类型“_InternalLinkedHashMap<String, dynamic>”不是“FutureOr<List<dynamic>>”类型的子类
Posted
技术标签:
【中文标题】如何修复此错误类型“_InternalLinkedHashMap<String, dynamic>”不是“FutureOr<List<dynamic>>”类型的子类型【英文标题】:how to fix this error type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>' 【发布时间】:2021-01-18 17:26:17 【问题描述】:您好,我是颤振飞镖的初学者。在这里,我试图从我的 API 显示列表视图,但出现错误。 错误
发生了异常。 _TypeError(类型 '_InternalLinkedHashMap
' 不是类型 'FutureOr' 的子类型)
我认为我的代码到目前为止还不错,但是 Future list 异步行代码有问题。
错误截图:Screenshot
代码:
class ShipsApps
int id;
String port_name;
int id_typort;
String description;
ShipsApps(this.id, this.port_name, this.id_typort, this.description);
factory ShipsApps.fromJson(Map<String, dynamic> json)
return ShipsApps(
id: json['id'],
port_name: json['port_name'],
id_typort: json['id_typort'],
description: json['description']);
class _hotelDetailState extends State<hotelDetail>
Future<List<dynamic>> getData() async
// final String apiURL = ;
var response =
await http.get('https://api.batulima.com/v1_ships/port_list');
return json.decode(response.body);
Widget build(BuildContext context)
double _height = MediaQuery.of(context).size.height;
double _width = MediaQuery.of(context).size.width;
var _relatedPostVar = Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 20.0, right: 20.0, top: 40.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Related Post",
style: TextStyle(
fontFamily: "Sofia",
fontSize: 20.0,
fontWeight: FontWeight.w700),
),
Text(
"See all",
style: TextStyle(
fontFamily: "Sofia",
fontSize: 16.0,
fontWeight: FontWeight.w300),
),
]),
),
SizedBox(
height: 10.0,
),
Container(
child: FutureBuilder<List>(
future: getData(),
builder: (context, snapshot)
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? new ItemList(
list: snapshot.data,
)
: new Center();
,
),
),
SizedBox(
height: 40.0,
),
],
);
Widget _relatedPost(String image, title, location, ratting)
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
height: 110.0,
width: 180.0,
decoration: BoxDecoration(
image:
DecorationImage(image: AssetImage(image), fit: BoxFit.cover),
color: Colors.black12,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
boxShadow: [
BoxShadow(
blurRadius: 5.0,
color: Colors.black12.withOpacity(0.1),
spreadRadius: 2.0)
]),
),
SizedBox(
height: 5.0,
),
Text(
title,
style: TextStyle(
fontFamily: "Sofia",
fontWeight: FontWeight.w600,
fontSize: 17.0,
color: Colors.black87),
),
SizedBox(
height: 2.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.location_on,
size: 18.0,
color: Colors.black12,
),
Text(
location,
style: TextStyle(
fontFamily: "Sofia",
fontWeight: FontWeight.w500,
fontSize: 15.0,
color: Colors.black26),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(
Icons.star,
size: 18.0,
color: Colors.yellow,
),
Padding(
padding: const EdgeInsets.only(top: 3.0),
child: Text(
ratting,
style: TextStyle(
fontWeight: FontWeight.w700,
fontFamily: "Sofia",
fontSize: 13.0),
),
),
// Text("(233 Rating)",style: TextStyle(fontWeight: FontWeight.w500,fontFamily: "Sofia",fontSize: 11.0,color: Colors.black45),),
SizedBox(
width: 35.0,
),
],
),
],
),
);
class ItemList extends StatelessWidget
final List list;
ItemList(this.list);
@override
Widget build(BuildContext context)
return ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: list.length,
itemBuilder: (context, i)
return _relatedPost(
"list[i]['port_name'].toString()", "list[i]['id'].toString()", "list[i]['id_typort'].toString()", "list[i]['description'].toString()");
,
);
【问题讨论】:
如果问题没有解决,请告诉我。 【参考方案1】:您的 api 返回一个 InternalLinkedHashMap,它是 Map 对象的子类型。 但是您的 FutureBuilder 假定您的 getData() 返回一个列表。 您的 api 返回类似于以下对象的内容:
success: "true",
data: [//data],
所以改用这个:
class ShipsApps
int id;
String port_name;
int id_typort;
String description;
ShipsApps(this.id, this.port_name, this.id_typort, this.description);
factory ShipsApps.fromJson(Map<String, dynamic> json)
return ShipsApps(
id: json['id'],
port_name: json['port_name'],
id_typort: json['id_typort'],
description: json['description']);
class _hotelDetailState extends State<hotelDetail>
Future<Map> getData() async
// final String apiURL = ;
var response =
await http.get('https://api.batulima.com/v1_ships/port_list');
return json.decode(response.body) as Map<String, dynamic>;
Widget build(BuildContext context)
double _height = MediaQuery.of(context).size.height;
double _width = MediaQuery.of(context).size.width;
var _relatedPostVar = Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 20.0, right: 20.0, top: 40.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Related Post",
style: TextStyle(
fontFamily: "Sofia",
fontSize: 20.0,
fontWeight: FontWeight.w700),
),
Text(
"See all",
style: TextStyle(
fontFamily: "Sofia",
fontSize: 16.0,
fontWeight: FontWeight.w300),
),
]),
),
SizedBox(
height: 10.0,
),
Container(
child: FutureBuilder<Map>(
future: getData(),
builder: (context, snapshot)
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? new ItemList(
list: snapshot.data["data"],
)
: new Center();
,
),
),
SizedBox(
height: 40.0,
),
],
);
Widget _relatedPost(String image, title, location, ratting)
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
height: 110.0,
width: 180.0,
decoration: BoxDecoration(
image:
DecorationImage(image: AssetImage(image), fit: BoxFit.cover),
color: Colors.black12,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
boxShadow: [
BoxShadow(
blurRadius: 5.0,
color: Colors.black12.withOpacity(0.1),
spreadRadius: 2.0)
]),
),
SizedBox(
height: 5.0,
),
Text(
title,
style: TextStyle(
fontFamily: "Sofia",
fontWeight: FontWeight.w600,
fontSize: 17.0,
color: Colors.black87),
),
SizedBox(
height: 2.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.location_on,
size: 18.0,
color: Colors.black12,
),
Text(
location,
style: TextStyle(
fontFamily: "Sofia",
fontWeight: FontWeight.w500,
fontSize: 15.0,
color: Colors.black26),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(
Icons.star,
size: 18.0,
color: Colors.yellow,
),
Padding(
padding: const EdgeInsets.only(top: 3.0),
child: Text(
ratting,
style: TextStyle(
fontWeight: FontWeight.w700,
fontFamily: "Sofia",
fontSize: 13.0),
),
),
// Text("(233 Rating)",style: TextStyle(fontWeight: FontWeight.w500,fontFamily: "Sofia",fontSize: 11.0,color: Colors.black45),),
SizedBox(
width: 35.0,
),
],
),
],
),
);
class ItemList extends StatelessWidget
final List list;
ItemList(this.list);
@override
Widget build(BuildContext context)
return ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: list.length,
itemBuilder: (context, i)
return _relatedPost(
"list[i]['port_name'].toString()", "list[i]['id'].toString()", "list[i]['id_typort'].toString()", "list[i]['description'].toString()");
,
);
【讨论】:
非常感谢先生,它有效。你救了我的命:') @Aphron 不客气 ;) 先生,您能再帮我一次吗?我需要你的帮助,但我不知道如何联系你。我有同样的错误。 这是我的电子邮件:abiriali78@gmail.com以上是关于如何修复此错误类型“_InternalLinkedHashMap<String, dynamic>”不是“FutureOr<List<dynamic>>”类型的子类的主要内容,如果未能解决你的问题,请参考以下文章
如何修复此错误未处理的异常:类型“字符串”不是“索引”类型“int”的子类型
如何修复此错误:未捕获(承诺)类型错误:无法读取未定义的属性(读取“长度”)
如何修复此错误类型“_InternalLinkedHashMap<String, dynamic>”不是“FutureOr<List<dynamic>>”类型的子类