类型 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型在颤振应用程序中出现此错误

Posted

技术标签:

【中文标题】类型 \'List<dynamic>\' 不是类型 \'Map<String, dynamic>\' 的子类型在颤振应用程序中出现此错误【英文标题】:type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' getting this error in flutter app类型 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型在颤振应用程序中出现此错误 【发布时间】:2021-05-24 15:30:40 【问题描述】:

我正在尝试从我的 Strapi 数据库列表中加载数据:http://localhost:1337/products 以下是我的数据库中的一个示例数据:

["图片":["_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" ,"格式":"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","re​​lated":["602d3287e790253a44f466e8"],"createdAt":"2021-02-17T15:13:03.648Z ","updatedAt":"2021-02-17T15:13:11.831Z","__v":0,"created_by":"60278359e1626337e009d550","updated_by":"60278359e1626337e009d550","id":"602d3247fe790253a4" ,"_id":"602d3287e790253a44f466e8","name":"PetroMax LPG","descript离子":"12 KG-22 毫米 || 35 KG-22 mm","Refill":800,"Cylinder":780,"createdAt":"2021-02-17T15:13:11.285Z","updatedAt":"2021-02-21T15:35:58.368 Z","__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",

我想在这个 product_item.dart 页面中显示每个产品:

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));
  


启动应用后出现以下错误:

E/flutter (25288): [ERROR:flutter/lib/ui/ui_dart_state.cc(184)] 未处理的异常:类型“List”不是“Map”类型的子类型 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/颤振(25288):#3 getProductsAction。 (包:flutter_ecommerce/redux/actions.dart:32:16)

这些是来自 actions.dart 文件的产品操作:

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);

这也是 product.dart 文件:

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']);
  

【问题讨论】:

你没有以正确的方式解析数据,请显示你从API收到的数据 我应该给你看哪一部分? 试试 Product.fromJson(productData['picture']); 仍然遇到同样的错误。未处理的异常:'List' 类型不是 'Map' 类型的子类型 E/flutter(6740):#0 getProductsAction..(package:flutter_ecommerce/redux /actions.dart:33:57) E/flutter (6740):#1 List.forEach (dart:core-patch/growable_array.dart:403:8) E/flutter (6740):#2 getProductsAction. (包:flutter_ecommerce/redux/actions.dart:32:16) @Miskaturrahman 一切 【参考方案1】:

我认为你从json.decode(response.body) 得到Map&lt;String, dynamic&gt;。但是您试图将其存储在List&lt;dynamic&gt; 中的getProductsAction() 方法中。你能改变类型并像下面这样检查吗?

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

【讨论】:

尝试出现以下错误:无法将参数类型“Null Function(String)”分配给参数类型“void Function(String, dynamic)”。 “参数类型'String'不能分配给参数类型'int'。”,“startLineNumber”:33,“startColumn”:24,“endLineNumber”:36,“endColumn”:4

以上是关于类型 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型在颤振应用程序中出现此错误的主要内容,如果未能解决你的问题,请参考以下文章

参数类型 'List<Series<dynamic, dynamic>>' 不能分配给参数类型 'List<Series<dynamic, String>&g

未处理的异常:类型 'List<dynamic>' 不是颤振中类型 'Map<dynamic, dynamic>' 的子类型

_TypeError(类型 'List<dynamic>' 不是类型 'Map<String, dynamic> 的子类型

类型 List<dynamic> 不是类型 'Map<String, dynamic>' 的子类型

需要一个“Map<List<dynamic>, dynamic>”类型的值,但在发出 api post 请求时得到一个“List<dynamic>”类型的值

未处理的异常:类型 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型