flutter 如何解决此错误未处理异常:类型“List< dynamic>”不是类型“Map〈String,dynamic>”的子类型

2vuwiymt  于 2023-02-20  发布在  Flutter
关注(0)|答案(1)|浏览(130)

在这里,我尝试使用http请求从PHP获取数据,我得到的数组JSON格式看起来像这样:

[
    {
        "productId": "8",
        "product_reference": "439588839",
        "product_title": "Fried potatoes",
        "price": "12.0",
        "buying_price": "5.0",
        "discounted_price": "0.0",
        "discription": "Slices of Potato, seasoning and sauce. Preparation Time: 20 min",
        "preparationTime": "0",
        "productImagePath": "productImages/IMG-produt63e3b504bb3664.13306114.jpg",
        "categoryId": "1",
        "unitId": "8",
        "product_status": "1",
        "createdOn": "2023-02-08 16:43:16",
        "udatedOn": "2023-02-08 16:43:16",
        "addedBy": "1"
    }
]

在Flutter中,我有一个函数可以帮助我获取Product Controller中名为fetchProduct()的数据,如下所示:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import 'package:restaurentapp/api_connection/api_connection.dart';
import 'package:restaurentapp/model/product_model.dart';

class ProductController extends GetxController {
  var productList = <ProductModel>[].obs;
  var isLoading = true.obs;

  @override
  void onInit() {
    super.onInit();
    fetchProduct();
  }

  Future<void> fetchProduct() async {
    final response = await http.get(Uri.parse(API.getCardDataProduct));
    // print("enockDataProduct:${response.body}");
    if (response.statusCode == 200) {
      ProductModel productModel = ProductModel.fromJson(jsonDecode(response.body));

      productList.add(
        ProductModel(
          productId: productModel.productId,
          title: productModel.title,
          product_reference: productModel.product_reference,
          product_price: productModel.product_price,
          buying_price: productModel.buying_price,
          product_description: productModel.product_description,
          product_category: productModel.product_category,
          unitId: productModel.unitId,
          productImagePath: productModel.productImagePath,
        ),
      );
      isLoading.value = true;
    }else{
      Get.snackbar("Error Loading data!", 'Server Response: ${response.statusCode}: ${response.reasonPhrase.toString()}');
    }
  }
}

在这个模型中我把动态Map串

class ProductModel {
  String productId;
  String title;
  String product_reference;
String product_price;
String buying_price;
String product_description;
String product_category;
String unitId;
String productImagePath;

  ProductModel({
    required this.productId,
    required this.title,
    required this.product_reference,
    required this.product_price,
    required this.buying_price,
    required this.product_description,
    required this.product_category,
    required this.unitId,
    required this.productImagePath,
  });

  factory ProductModel.fromJson(Map<String, dynamic> json) {
    return ProductModel(
      productId: json['productId'],
      title: json['product_title'],
      product_reference: json['product_reference'],
      product_price: json['product_price'],
      buying_price: json['buying_price'],
      product_description: json['product_description'],
      product_category: json['product_category'],
      unitId: json['unitId'],
      productImagePath: json['productImagePath'],
    );
  }
}

求你了,我需要这个求你帮帮我,尽快帮我,求你了.

dxxyhpgq

dxxyhpgq1#

在这里,您试图将列表类型转换为Map,这是无效的。您需要使用以下代码更新解码行。
List<ProductModel>.from(jsonDecode(response.body).map((x) => ProductModel.fromJson(x)))
它会从Json直接带来一个List,你不需要添加一个模型到这个List。

Future<void> fetchProduct() async {
  final response = await http.get(Uri.parse(API.getCardDataProduct));
  // print("enockDataProduct:${response.body}");
  if (response.statusCode == 200) {
    final productList = List<ProductModel>.from(
        jsonDecode(response.body).map((x) => ProductModel.fromJson(x)));

    productList.addAll(productList);
    isLoading.value = true;
  } else {
    Get.snackbar("Error Loading data!",
        'Server Response: ${response.statusCode}: ${response.reasonPhrase.toString()}');
  }
}

相关问题