1

Why I'm getting this exception:

_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>')

enter image description here

this is my http method:

import 'dart:convert';
import 'package:arzenafees/model/areaguide.dart';
import 'package:http/http.dart' as http;

Future<Areaguide> fetcharea() async {
  final response = await http.get(
      Uri.parse('https://arz-e-nafees.nafeessolutions.com/public/api/view'));
  if (response.statusCode == 200) {
    return Areaguide.fromJson(jsonDecode(response.body));
  } else {
    throw Exception('Unexpected error occured!');
  }
}

this is my model class which I created using quicktype.io:

import 'dart:convert';

List<Areaguide> areaguideFromJson(String str) =>
    List<Areaguide>.from(json.decode(str).map((x) => Areaguide.fromJson(x)));

String areaguideToJson(List<Areaguide> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Areaguide {
  Areaguide({
    required this.propertyImage,
    required this.propertyTitle,
    required this.locationCity,
    required this.locationArea,
    required this.propertyDescription,
    required this.propertyPrice,
  });

  String propertyImage;
  String propertyTitle;
  String locationCity;
  String locationArea;
  String propertyDescription;
  String propertyPrice;

  factory Areaguide.fromJson(Map<String, dynamic> json) => Areaguide(
        propertyImage: json["property_image"],
        propertyTitle: json["property_title"],
        locationCity: json["location_city"],
        locationArea: json["location_area"],
        propertyDescription: json["property_description"],
        propertyPrice: json["property_price"],
      );

  Map<String, dynamic> toJson() => {
        "property_image": propertyImage,
        "property_title": propertyTitle,
        "location_city": locationCity,
        "location_area": locationArea,
        "property_description": propertyDescription,
        "property_price": propertyPrice,
      };
}

please provide answer which applies to all related question, do not limits it to this specific problem, which might help others with type errors.

5
  • You want to display above json data inside widget? Commented Jun 8, 2022 at 5:43
  • 1
    If you getting data from API and display it into Flutter. refer my answers Answer 1, Answer 2, Answer 3, Answer 4, Answer 5 and official documentation here, I have try your solution in other format it working good but some images gives error they are not display Commented Jun 8, 2022 at 6:07
  • 1
    Since we do not know what JSON you get we don't know what's wrong. Can you post the JSON you get from the API? The most obvious guess would be that your API does indeed not deliver one item, but a list of items. If that were the case, what would you like to do with it? Commented Jun 8, 2022 at 6:14
  • @nvoigt yes u r right, api is giving list of maps, will you please check Maqsood answer, it is working but not explained Commented Jun 9, 2022 at 5:30
  • @Ravindra S. Patil, yes, some images are just network path and some are base64encode, will you please tell me how to show base64encode image in cards from api, maqsood answer is working, but I want to get image not image path i.e network path Commented Jun 9, 2022 at 5:32

2 Answers 2

2

Try this.

static Future<List<Areaguide>?> fetcharea() async {
       
        final response = await http.get(
            Uri.parse('https://arz-e-nafees.nafeessolutions.com/public/api/view));
                if (response.statusCode == 200) {
                  List<Areaguide> property = (json.decode(response.body)).map<Areaguide>((m)=> Areaguide.fromJson(m)).toList();
                  return property;
            } else {
            throw Exception('Unexpected error occured!');
            }
        }
Sign up to request clarification or add additional context in comments.

2 Comments

yes, your code is working, but please explain me how u resolve it by adding this line:.map<Areaguide>((m)=> Areaguide.fromJson(m)).toList() and how it is working, after that I will mark your answer accepted and upvote it.
Simple list of objects is coming in your (response.body). I simple map that list of objects coming in response with your Model (Areaguide) and parse it with your fromJson and made it a list of Areaguide.
0

Instead of writing fromJson and toJson methods, you can use json_serializable library (https://docs.flutter.dev/development/data-and-backend/json). In short: after installing the dependency you just define your model and run a command. It works also for nested models.

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.