0

Below code return type'List' is not a subtype of type 'Map<String, dynamic>

    import 'dart:convert';
import 'package:fewis/models/vacancy.dart';
import 'package:http/http.dart' as http;

class VacancyApi {
  static Future<List<VacancyModel>> getVacancy() async {
    String url = "https://digitalfewis.com/api/vacany_information";
    try {
      final response = await http.get(Uri.parse(url));
      if (response.statusCode == 200) {
        // return vacancyModelFromJson(response.body);
        List jsonResponse = json.decode(response.body);
        return jsonResponse
            .map((data) => new VacancyModel.fromJson(data))
            .toList();
      } else {
        throw Exception('Slow Network Connection');
      }
    } catch (e) {
      rethrow;
    }
  }
}

[I was fetching Data from Api and i have made Json Model class but when i tried to convert the json to list it gives me an error 'List' is not a subtype of type 'Map<String, dynamic>')

1
  • 1
    Next time Adding the question also add which API you are hitting and what is the response you get so we can understand better. The answer given by @Rahul is right. Commented May 6, 2022 at 6:06

1 Answer 1

1

Your response is List of List.

You might want to do

List jsonResponse = json.decode(response.body);
if(jsonResponse.isNotEmpty) {
    List vacanciesJson = jsonResponse[0];
    return vacanciesJson
            .map((data) => VacancyModel.fromJson(data))
            .toList();
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.