0

I am trying to convert my data from Json to a Map but I am getting this error that says type 'List' is not a subtype of type 'String'. I am actually returning a list of route as contained in the server.I had did something similar to this by customizing fromJson and toJson method to convert whatever thing that I need to either send or retrieved without using jsonDecode and jsonEncode and it was working perfectly, I don't really know why this is not working. I am just a beginner in flutter. Here is what I tried

            class RouteModel{
              int? id;
              String? location;
              String? destination;
              String? takeOffTime;
              String? arrivalTime;
            
              RouteModel({this.id, this.location, this.destination, this.takeOffTime,
                  this.arrivalTime});
            
              RouteModel.fromJson(Map<String, dynamic> json){
                id = json['id'];
                location = json['takeOffLocation'];
                destination = json['destination'];
                takeOffTime = json['takeOffTime'];
                arrivalTime = json['arrivalTime'];
              }
            }
            
            class FlightRouteListRepository extends GetxService{
            
              final ApiClient apiClient;
              FlightRouteListRepository({required this.apiClient});
            
              Future<Response> getAllRouteList() async {
                return await apiClient.getData(AppUrlConstant.ROUTE_LIST_URI);
              }
            
              }
            
            
            class FlightRouteListController extends GetxController{
              final FlightRouteListRepository flightRouteListRepository;
              FlightRouteListController({required this.flightRouteListRepository});
            
              List<RouteModel> _routeModelList = [];
              List<RouteModel> get routeModelList => _routeModelList;
            
            bool _isLoaded = false;
              bool get isLoaded => _isLoaded;
            
              Future<void> getAllFlightRoute()async{
            
                Response response = await flightRouteListRepository.getAllRouteList();
                try{
                      _isLoaded = true;
                      update();
                      if(response.statusCode==200){
                        print("Got route from the server");
                        _routeModelList = [];
                        _routeModelList.add(RouteModel.fromJson(response.body));
                        print(_routeModelList);
            
                      }else{
                        print("The response code is: "+response.statusCode.toString());
                        print("The error is: "+response.statusText!);
                      }
                }catch(e){
                  print(e.toString());
                }
                _isLoaded =false;
                update();
              }
Ukeme Elijah
  • 157
  • 3
  • 13

2 Answers2

1

Make these changes:

if(response.code == 200){
    List<dynamic> responseBody = jsonDecode(response.body);
    _routeModelList = responseBody.map((e) => RouteModel.fromJson(e)).toList();
  }
Vivek Chib
  • 837
  • 1
  • 2
  • 13
  • I tried this your solution @Vivek Chib but it's still giving me the same error message of type 'List' is not a subtype of type 'String'. The main issue is how to loop through the response and add it to the RouteModel because my server returns a list of RouteModel for me which is exactly what I was expected. When I print response.body.toString in the console, it returns a list of response for me which is the correct data as found in the server but I cannot loop through it so that I can add each of the data in my RouteModel class. Hopefully you understood my question – Ukeme Elijah Jul 16 '23 at 05:53
  • would you mind adding Json response to your post. It'll narrow down the problem. – Vivek Chib Jul 16 '23 at 12:21
0

Make sure you're feeding proper response.body in the code below:

RouteModel.fromJson(response.body)

Debugging definitely would help here.

Most likely your issue is one of these variables

String? location;
String? destination;
String? takeOffTime;
String? arrivalTime;

in response.body is type of List (array), not String, as it's suggesting.

Using tools like POSTMAN would help here too, to see what's actually coming from the API.

Hope this helps.

ermekk
  • 66
  • 5
  • The issue is not actually from the response as it's returning a list of all the data I had in my server when I print response.body.toString in my console but the issue is about looping through the response in order to add each of the response in my RouteModel object. The response from the server is correct @emekk. As the error message suggested, I am getting a list of data not a single one and I want to loop through the data gotten from the response one by one and add it to the RouteModel object. – Ukeme Elijah Jul 16 '23 at 05:58
  • like this? `final responseItems = response.body as List;` then `final items = []; for (final item in responseItems) { items.add(RouteModel.fromJson(item)); }` – ermekk Jul 17 '23 at 09:20
  • Thanks @ermekk, this last solution solved the problem for me – Ukeme Elijah Jul 18 '23 at 04:13