1

Sorry if It's repeated the question but I didn't found a solution.

Error:

Exception: type '(dynamic) => PostalModel' is not a subtype of type '(Map<String, dynamic>) => List' of 'f'

What I have done:

  1. Calling API:

    Future<PostalModel> fetchPhotos(http.Client client) async {
       final response =
        await client.get('https://api.postalpincode.in/pincode/364710');
    
       // Use the compute function to run parsePhotos in a separate isolate.
       return compute(parsePostalPincode, response.body);
    }
    
  2. parsing JSON:

     // A function that converts a response body into a List.
     PostalModel parsePostalPincode(String responseBody) {
       print(responseBody);
       final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();
       print('Parsed : $parsed'); //Getting Log OUTPUT
       PostalModel postalModel = parsed.map<List<PostalModel>>((json) => PostalModel.fromJson(json));
       print('Postal Model : $postalModel'); //Getting Log OUTPUT
       return postalModel;
     }
    
  3. Binding Data in body:

    body: FutureBuilder<PostalModel>(
      future: fetchPhotos(http.Client()),
      builder: (context, snapshot) {
        print('snapshot $snapshot'); // Getting Error Here
        // "AsyncSnapshot<PostalModel>(ConnectionState.done, null, Exception: type '(dynamic) => PostalModel' is not a subtype of type '(Map<String, dynamic>) => List<PostalModel>' of 'f')"
        if (snapshot.hasError) print(snapshot.error);
    
        return snapshot.hasData
           ? PostOfficeList(postOfficeList: snapshot.data.postOffice)
           : Center(child: CircularProgressIndicator());
      },
    ),
    

You can check JSON from API Link and I have created a Model class from here.

0

1 Answer 1

0

And finally, I got succeed with the following solution:

// A function that converts a response body into a List.
PostalModel parsePostalPincode(String responseBody) {
  final parsed = jsonDecode(responseBody).cast<Map<String, dynamic>>();
  print('Parsed : $parsed[0]'); 
  PostalModel postalModel = PostalModel.fromJson(parsed[0]);
  print('Postal Model : $postalModel');
  return postalModel;
}

Flutter is so crazy for a newbie. :P

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.