Reputation: 103
Hello I'm a beginner in flutter dart. Here I'm trying to show the listview from my API and I got an error. Error
Exception has occurred. _TypeError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List>')
and I think my code so far is fine, but there is something wrong in Future list async line code.
Error Screenshot:
Screenshot
Code:
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()");
},
);
}
}
Upvotes: 0
Views: 131
Reputation: 552
Your api returns an InternalLinkedHashMap which is a subtype of Map object. But your FutureBuilder assumes that your getData() returns a List. Your api returns something like object below:
{
success: "true",
data: [//data],
}
So use this instead:
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()");
},
);
}
}
Upvotes: 1