When i am trying to fetch api this error : "type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>" shows up

Romanch haque

I am trying to create a API request in Flutter but I'm getting following error as response

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

I am trying to create first API and kindly let me know if the approach is fine here is my code:

Future<Welcome> fetchProduct() async {
  final response = await http.get(Uri.parse(
      'https://api.azabazar.com/api/all-products/?fbclid=IwAR0v4BAB_gFTcpU7Jjqg_t0MnUkUxq8sWPgLj1BHrmOOq_Nd132mz9F8iSU'));

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    return Welcome.fromJson(jsonDecode(response.body));
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}

class model:

class Welcome {
  final String title;
  final DateTime date;
  final String image;
  final String description;
  final int category;
  final int price;

  Welcome({
    required this.title,
    required this.date,
    required this.image,
    required this.description,
    required this.category,
    required this.price,
  });

  factory Welcome.fromJson(Map<String, dynamic> json) {
    return Welcome(
      title: json["title"],
      date: DateTime.parse(json["date"]),
      image: json["image"],
      description: json["description"],
      category: json["category"],
      price: json["price"],
    );
  }

Here is my sample API

[
    {
        "title": "iphone 6s",
        "date": "2021-07-11T15:51:35.289193Z",
        "image": "/media/products/IMG_20210227_002515.jpg",
        "description": "iphone 6s \r\nDisplay: 4.7\"\r\nRam-2GB\r\nROM-64GB",
        "category": 2,
        "price": 10000
    },
    {
        "title": "iphone 6s again",
        "date": "2021-07-11T15:52:22.457555Z",
        "image": "/media/products/IMG_20210227_002515_jtKsLrL.jpg",
        "description": "iphone 6s \r\nscru chara phone",
        "category": 2,
        "price": 5500
    },
    {
        "title": "pegion",
        "date": "2021-08-09T16:37:18.410878Z",
        "image": "/media/products/download_gQUBVZK.png",
        "description": "this is a pegion, cinese owal",
        "category": 3,
        "price": 3000
    },
    {
        "title": "pulser",
        "date": "2021-08-25T06:14:58.402376Z",
        "image": "/media/products/50340732_332072787517362_7912562101313339392_n.jpg",
        "description": "bajaj pulser ,fbhfubfj",
        "category": 4,
        "price": 100000
    }
]

I've also used futureBuilder :

  late Future<Welcome> futureProduct;
  @override
  void initState() {
    super.initState();
    futureProduct = fetchProduct();
  }
mmcdon20

It is because your API does not return a single Welcome item but rather multiple Welcome items.

You can adjust the fetchProduct method to return a List<Welcome>:

Future<List<Welcome>> fetchProducts() async {
  final response = await http.get(Uri.parse(
      'https://api.azabazar.com/api/all-products/?fbclid=IwAR0v4BAB_gFTcpU7Jjqg_t0MnUkUxq8sWPgLj1BHrmOOq_Nd132mz9F8iSU'));

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    return [
      for (final item in jsonDecode(response.body)) Welcome.fromJson(item),
    ];
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Api type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

Constant Error - Type List Dynamic Is Not A Subtype Of Type Map String Dynamic

Why doesn't parsing work? Error type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>

in Dart ERROR : type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in Flutter with Firestore

I/flutter (10710): type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

Can't convert Map to List in Dart, facing "Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>" error

Flutter - Stripe : type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>?' error in method_channel_stripe.dart:263

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

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

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

Type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

type list dynamic is not a subtype of type map string dynamic

type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic in flutter app

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

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

Type List<dynamic> is not a subtype of type Map<String dynamic>

Flutter: Type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

type List<dynamic> is not a subtype of type Map<String, dynamic>

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' flutter

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' While fetching Json from jsonplaceholder.typecode.com/photos

Why am I getting this error "TypeError: string indices must be integers" when trying to fetch data from an api?

TypeError: 'NoneType' object is not callable: This shows up when I am trying to work with an excel file in Python using openpyxl

Code is not working when i am trying to edit the API URL by data entered in form and fetch data from API

when i am cloning in to website it shows error

React When i am trying to get into a nested object the fetch throws me an undefined error

When I am trying to push code on Git it shows errors

Error when i am trying to remove an element

I am getting a type error when I am trying to replace

I am getting null values when I select from dropdown in php. And sometime, it shows up error like "undefined index"

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive