Home > front end >  type 'List<dynamic>' is not a subtype of type 'List<LastLocationResModel>
type 'List<dynamic>' is not a subtype of type 'List<LastLocationResModel>

Time:02-18

here is my repository to get data from the API, and I got the error, in the bloc state management I try to use anyways but it still got the error.

I'm getting this error

type 'List' is not a subtype of type 'List'

import 'dart:convert';

import 'package:gms_mobile/api/api-constants.dart';
import 'package:gms_mobile/api/api-mspf.dart';
import 'package:gms_mobile/exception/page_exception.dart';
import 'package:gms_mobile/features/last_location/model/last-location-response-model.dart';

class LastLocationRepository {
  Future<List<LastLocationResModel>> doGetLastLocation() async {
    try {
      List<LastLocationResModel> LocationList = [];
      var response = await ApiMspf.getinstance
          .get(ApiServiceConstants.last_location   '/10115795/route');
      print(response);
      try {
        // LocationList = response;
        LocationList = response.map((item) {
          LocationList.add(new LastLocationResModel(
              lat: item["lat"],
              lon: item["lon"],
              timestamp: item["timestamp"]));
        }).toList();
        return LocationList;
      } catch (error) {
        throw ModelingException(message: error.toString());
      }
    } catch (error) {
      rethrow;
    }
  }
}

CodePudding user response:

You need to try this

class LastLocationRepository {
  Future<List<LastLocationResModel>> doGetLastLocation() async {
    try {
      List<LastLocationResModel> LocationList = [];
      var response = await ApiMspf.getinstance
          .get(ApiServiceConstants.last_location   '/10115795/route');
      print(response);
      try {
          var decode = jsonDecode(response.body);
          decode.forEach((val){
             LocationList.add(LastLocationResModel(
               lat: item["lat"],
               lon: item["lon"],
               timestamp: item["timestamp"]))
          });
        return LocationList;
      } catch (error) {
        throw ModelingException(message: error.toString());
      }
    } catch (error) {
      rethrow;
    }
  }
}

Main part is below which add all data in your list and then return this list

 var decode = jsonDecode(response.body);
          decode.forEach((val){
            LocationList.add(LastLocationResModel(
                lat: item["lat"],
                lon: item["lon"],
                timestamp: item["timestamp"]))
          });
          return LocationList;

CodePudding user response:

Here is my model

class LastLocationResModel {
  LastLocationResModel({
    required this.lat,
    required this.lon,
    required this.timestamp,
  });

  double lat;
  double lon;
  int timestamp;

  factory LastLocationResModel.fromJson(Map<String, dynamic> json) => LastLocationResModel(
    lat: json["lat"].toDouble(),
    lon: json["lon"].toDouble(),
    timestamp: json["timestamp"],
  );

  Map<String, dynamic> toJson() => {
    "lat": lat,
    "lon": lon,
    "timestamp": timestamp,
  };
}
  • Related