Converting Json in flutter

96 views Asked by At

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();
              }
2

There are 2 answers

2
Vivek Chib On

Make these changes:

if(response.code == 200){
    List<dynamic> responseBody = jsonDecode(response.body);
    _routeModelList = responseBody.map((e) => RouteModel.fromJson(e)).toList();
  }
2
ermekk On

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.