简体   繁体   中英

Flutter Firebase error in read data: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast

在此处输入图像描述 Error: Exception has occurred. _CastError (type 'List' is not a subtype of type 'Map<String, dynamic>' in type cast)

How can i solve this problem??

Edit: 'extractedData' is Map, how can i convert it to List to add it in 'final List loadedProducts = [];'?

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/foundation.dart';

class ProductMain with ChangeNotifier{
  final String idFood;
  final String nameFood;
  // ignore: non_constant_identifier_names
  final String ImgUrl;

  ProductMain({
    @required this.idFood,
    this.nameFood,
    // ignore: non_constant_identifier_names
    this.ImgUrl,
  });
}

for the post required: "It looks like your post is mostly code; please add some more details." "It looks like your post is mostly code; please add some more details."

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:app/product_main_data.dart';

import 'package:http/http.dart' as http;

class ProductsMainPV with ChangeNotifier {
  List<ProductMain> _items = [];

  List<ProductMain> get items {
    return [..._items];
  }

  ProductMain findById(String idFood) {
    return _items.firstWhere((prod) => prod.idFood == idFood);
  }



  Future<void> fetchAndSetProducts() async {
    const url =
        'https://app.firebaseio.com/productList.json';
    try {
      final response = await http.get(url);
      final extractedData = json.decode(response.body);
      final List<ProductMain> loadedProducts = [];
      extractedData.forEach((product) {
        print(product);
        loadedProducts.add(ProductMain(
          idFood: product,
          nameFood: product['nameFood'],
          ImgUrl: product['ImgUrl'],
        ));
      });
      _items = loadedProducts;
      notifyListeners();
    } catch (error) {
      throw (error);
    }
  }

  void updateProduct(String id, ProductMain newProduct) {
    final prodIndex = _items.indexWhere((prod) => prod.idFood == id);
    if (prodIndex >= 0) {
      _items[prodIndex] = newProduct;
      notifyListeners();
    } else {
      print('...');
    }
  }
}

Output:

product is null

I am not sure about this, since your question doesn't contain the structure of response recieved. But give this a try.

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

Since you are listing all products, it maybe list of items. So casting it to Map doesn't work!

final extractedData = json.decode(response.body);
......
extractedData.forEach((product) {
   print(product);
   // Do Something!
}

Looks like your response.body is a List not a Map. What are you expecting the url call to return? You can test it before processing by using eg. If (response.body is Map)...else if (response.body is List)... if it is a Map, process it as a Map otherwise process it as a List.

Update based on comments It's a list of maps. So you need to iterate over the list and process each map, probably iterating over each map. So a couple of for-in or for-each loops are needed. As per the other answer, print out each iteration to see what you have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM