简体   繁体   中英

Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast in request flutter

Hi I am getting this error. As I understand it, it appears when a request is made to the server, I don't understand why. By the way, I tried to find the answer here, but unfortunately, either the answers are not clear to me, or they do not suit me.

Here is some information that might be useful to you

  • Response from the server in the form - 回复

Model -

factory DetailsModel.fromJson(Map<String, dynamic> json) {
    return DetailsModel(
        cpu: json['CPU'],
        camera:json['camera'],
        id: json['id'],
        price: json['price'],
        rating: json['rating'],
        sd: json['sd'],
        ssd:json['ssd'],
        title: json['title'],
        capacity: json['capacity'],
        color: json['color'],
        images:json['images']
    );
  }

Request -

class DetailsRemoteDataSorceImpl implements DetailsRemoteDataSorce {
  final http.Clientclient;

  DetailsRemoteDataSorceImpl({required this.client});
  
  @override
  Future<List<DetailsModel>> getAllDetails() async {
    final response = await http.get(
        Uri.parse(ConfigUrl.details),
        headers: {'Content-Type': 'application/json'}
    );
    if(response.statusCode == 200) {
       final details = json.decode(response.body);
      return (details as List).map((e) => DetailsModel.fromJson(e)).toList();
    } else {
      throw ServerException();
    }
  }
}

And here's how I'm trying to display -

Text(details[0].title,)

response.body -

Result response.body: {CPU: Exynos 990, camera: 108 + 12 mp, capacity: [126, 252], color: [#772D03, #010035], id: 3, images: [https://avatars.mds.yandex.net/get-mpic/5235334/img_id5575010630545284324.png/orig, https://www.manualspdf.ru/thumbs/products/l/1260237-samsung-galaxy-note-20-ultra.jpg], isFavorites: true, price: 1500, rating: 4.5, sd: 256 GB, ssd: 8 GB, title: Galaxy Note 20 Ultra}

Please help, I spent about 3-4 hours on this

as i see your model accepts some lists so you have to convert your model like this below:

class DetailsModel {
  String? cPU;
  String? camera;
  List<String>? capacity;
  List<String>? color;
  String? id;
  List<String>? images;
  bool? isFavorites;
  int? price;
  double? rating;
  String? sd;
  String? ssd;
  String? title;

  DetailsModel(
      {this.cPU,
      this.camera,
      this.capacity,
      this.color,
      this.id,
      this.images,
      this.isFavorites,
      this.price,
      this.rating,
      this.sd,
      this.ssd,
      this.title});

  DetailsModel.fromJson(Map<String, dynamic> json) {
    cPU = json['CPU'];
    camera = json['camera'];
    capacity = json['capacity'].cast<String>();
    color = json['color'].cast<String>();
    id = json['id'];
    images = json['images'].cast<String>();
    isFavorites = json['isFavorites'];
    price = json['price'];
    rating = json['rating'];
    sd = json['sd'];
    ssd = json['ssd'];
    title = json['title'];
  }} 

and your request will be somthing like this:)

  getAllDetails() async {
    final response = await http.get(
        Uri.parse(ConfigUrl.details),
        headers: {'Content-Type': 'application/json'}
    );
    if(response.statusCode == 200) {
       final details = json.decode(response.body);
      return DetailsModel.fromJson(details));
    } else {
      throw ServerException();
    }
  }

I hope it will work:)

As you can see in your api response, it is returning a single object not list of them, if you expect a list of item maybe you should contact backend developer, but if receive what you want, you should do this:

if(response.statusCode == 200) {
       List<DetailsModel> result =[];
       DetailsModel detail = DetailsModel.fromJson(json.decode(response.body))
       result.add(detail);
       return result;
    } else {
      throw ServerException();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM