如何解决错误类型'List <dynamic>'不是类型'Map <String,dynamic>'

时间:2020-03-14 20:04:20

标签: json api flutter

你好,我是Dart编程和JSON的新手。我正在使用Flutter开发我的应用程序。它需要从API获取数据。当我在Flutter中转到页面上的URL来获取数据时,它显示了一个错误。我该如何解决这个错误?

Rav类

class detalisRav {
  int id;
  String nameravs;
  String date_from;
  String detalis;


  detalisRav({
    this.id,
    this.nameravs,
    this.date_from,
    this.detalis,

  });


  factory detalisRav.fromjson(Map<String, dynamic> json){
    return detalisRav(
      id: json['id'],
      nameravs: json['nameravs'],
      date_from: json['date_from'],
      detalis: json['detalis'],

    );
  }
}   

函数调用API

  Future<detalisRav> Showdetalis(int id ) async{
    var token = await read();

    final String url= 'showdetalis/$id';
    String FullURL = Serveurl+url;
    var response =await http.post(FullURL,

        headers: {HttpHeaders.contentTypeHeader: "application/json", HttpHeaders.authorizationHeader: "Bearer $token"}
    );
    print('Token : ${token}');
    print(response);
    if (response.statusCode==200) {
      print('data:${response.body}');

         return detalisRav.fromjson(json.decode(response.body));
    }
    else{
      throw Exception('Failed to load data from Server.');
    }

  }

显示数据

    return FutureBuilder<detalisRav>(
      future:helper.Showdetalis(int.parse(widget.id)),
      builder: (context,snaphost){
        if (snaphost.hasData)
          return Center(

            child: Text(snaphost.data.nameravs),
          );
        else if (snaphost.hasError){
print('error:${snaphost.error}');
          return Text("${snaphost.error}");

        }
        return CircularProgressIndicator();

      },
    );
  }

使用的JSON

[
    {
        "id": 72,
        "nameravs": "ahmed ali ",
        "image": "https://fathomless-brushlands-95996.herokuapp.com/images_ravs/1584134167.jpg",
        "Captenname": "eng.ali",
        "detalis": "Sentence"
    }
]

错误

错误error.type“列表”不是“ Map”类型的子类型

0 个答案:

没有答案