user8303579
user8303579

Reputation:

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

I am trying to load data from my strapi database list that is: http://localhost:1337/products Below is one sample data from my database:

[{"picture":[{"_id":"602d327fe790253a44f466e7","name":"petromax.png","alternativeText":"","caption":"","hash":"petromax_14618add72","ext":".png","mime":"image/png","size":73.87,"width":444,"height":512,"url":"/uploads/petromax_14618add72.png","formats":{"thumbnail":{"name":"thumbnail_petromax.png","hash":"thumbnail_petromax_14618add72","ext":".png","mime":"image/png","width":135,"height":156,"size":30.2,"path":null,"url":"/uploads/thumbnail_petromax_14618add72.png"},"small":{"name":"small_petromax.png","hash":"small_petromax_14618add72","ext":".png","mime":"image/png","width":434,"height":500,"size":205.37,"path":null,"url":"/uploads/small_petromax_14618add72.png"}},"provider":"local","related":["602d3287e790253a44f466e8"],"createdAt":"2021-02-17T15:13:03.648Z","updatedAt":"2021-02-17T15:13:11.831Z","__v":0,"created_by":"60278359e1626337e009d550","updated_by":"60278359e1626337e009d550","id":"602d327fe790253a44f466e7"}],"_id":"602d3287e790253a44f466e8","name":"PetroMax LPG","description":"12 KG-22 mm || 35 KG-22 mm","Refill":800,"Cylinder":780,"createdAt":"2021-02-17T15:13:11.285Z","updatedAt":"2021-02-21T15:35:58.368Z","__v":0,"created_by":{"_id":"60278359e1626337e009d550","username":null,"firstname":"DokanDar","lastname":"eComm","createdAt":"2021-02-13T07:44:25.817Z","updatedAt":"2021-02-13T07:44:26.352Z","__v":0,"id":"60278359e1626337e009d550"},"updated_by":{"_id":"60278359e1626337e009d550","username":null,"firstname":"DokanDar","lastname":"eComm","createdAt":"2021-02-13T07:44:25.817Z","updatedAt":"2021-02-13T07:44:26.352Z","__v":0,"id":"60278359e1626337e009d550"},"Price":850,"id":"602d3287e790253a44f466e8"},

I want to show each of the products in this product_item.dart page:

import 'package:flutter/material.dart';
import 'package:flutter_ecommerce/models/app_state.dart';
import 'package:flutter_ecommerce/models/product.dart';
import 'package:flutter_redux/flutter_redux.dart';

class ProductItem extends StatelessWidget {
  final Product item;
  ProductItem({this.item});

  @override
  Widget build(BuildContext context) {
    final String pictureUrl = 'http://localhost:1337${item.picture['url']}';
    return GridTile(
        footer: GridTileBar(
            title: FittedBox(
                fit: BoxFit.scaleDown,
                alignment: Alignment.centerLeft,
                child: Text(item.name, style: TextStyle(fontSize: 20.0))),
            subtitle: Text("\$${item.price}", style: TextStyle(fontSize: 16.0)),
            backgroundColor: Color(0xBB000000),
            trailing: StoreConnector<AppState, AppState>(
                converter: (store) => store.state,
                builder: (_, state) {
                  return state.user != null
                      ? IconButton(
                          icon: Icon(Icons.shopping_cart),
                          color: Colors.white,
                          onPressed: () => print('pressed'))
                      : Text('');
                })),
        child: Image.network(pictureUrl, fit: BoxFit.cover));
  }
}

After starting the app am getting following error:

E/flutter (25288): [ERROR:flutter/lib/ui/ui_dart_state.cc(184)] Unhandled Exception: type 'List' is not a subtype of type 'Map<String, dynamic>' E/flutter (25288): #0 new Product.fromJson (package:flutter_ecommerce/models/product.dart:23:22) E/flutter (25288): #1 getProductsAction.. (package:flutter_ecommerce/redux/actions.dart:33:37) E/flutter (25288): #2 List.forEach (dart:core-patch/growable_array.dart:403:8) E/flutter (25288): #3 getProductsAction. (package:flutter_ecommerce/redux/actions.dart:32:16)

These are product actions from actions.dart file:

ThunkAction<AppState> getProductsAction = (Store<AppState> store) async {
  http.Response response = await http.get('http://localhost:1337/products');
  final List<dynamic> responseData = json.decode(response.body);
  List<Product> products = [];
  responseData.forEach((productData) {
    final Product product = Product.fromJson(productData);
    products.add(product);
  });
  store.dispatch(GetProductsAction(products));
};

class GetProductsAction {
  final List<Product> _products;

  List<Product> get products => this._products;

  GetProductsAction(this._products);
}

This is also product.dart file:

import 'package:meta/meta.dart';

class Product {
  String id;
  String name;
  String description;
  num price;
  Map<String, dynamic> picture;

  Product(
      {@required this.id,
      @required this.name,
      @required this.description,
      @required this.price,
      @required this.picture});

  factory Product.fromJson(Map<String, dynamic> json) {
    return Product(
        id: json['id'],
        name: json['name'],
        description: json['description'],
        price: json['price'],
        picture: json['picture']);
  }
}

Upvotes: 0

Views: 871

Answers (1)

Ashok Kuvaraja
Ashok Kuvaraja

Reputation: 716

I think you are getting Map<String, dynamic> from json.decode(response.body). but you are trying to store it in List<dynamic> inside the getProductsAction() method. Can you change the type and check like below?

final Map<String, dynamic> responseData = json.decode(response.body);

Upvotes: 0

Related Questions