如何解决此错误类型'_InternalLinkedHashMap <String,dynamic>'不是'FutureOr <List <dynamic >>'类型的子类型

时间:2020-10-03 07:09:41

标签: flutter dart

你好,我是扑镖的初学者。在这里,我试图从我的API中显示列表视图,但出现错误。 错误

发生异常。 _TypeError(类型'_InternalLinkedHashMap '不是类型'FutureOr '的子类型)

我认为到目前为止,我的代码还可以,但是“将来”列表异步行代码中存在错误。

错误屏幕截图:
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 个答案:

答案 0 :(得分: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()");
  },
);
}
}