Home > OS >  flutter: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type �
flutter: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type �

Time:09-08

Im new to the flutter but I can't find a solution to fix this.

     Future<void> fetchAndSetProducts([bool filterByUser = false]) async {
    final filterString =
        filterByUser ? 'orderBy"userId"&equalTo="${userId}"' : '';
    final Uri url = Uri.parse(
        'https://flutter-update-8df10-default-rtdb.europe-west1.firebasedatabase.app/products.json?auth=${authToken}&${filterString}');
    try {
      final response = await http.get(url);
      final extractedData = json.decode(response.body) as Map<String, dynamic>;
      if (extractedData == null) {
        print('extractedDataProducts = null');
        return;
      }
      final Uri favoritesResponseUrl = Uri.parse(
          'https://flutter-update-8df10-default-rtdb.europe-west1.firebasedatabase.app/UserFavorites/${userId}.json?auth=${authToken}');
      final favoriteResponse = await http.get(url);
      Map<String, dynamic> _favoriteData =
          Map<String, dynamic>.from(json.decode(favoriteResponse.body));
      final List<Product> loadedProducts = [];
      extractedData.forEach((prodId, prodData) {
        loadedProducts.add(Product(
          id: prodId,
          title: prodData['title'],
          description: prodData['description'],
          price: prodData['price'],
          isFavorite: _favoriteData == null ? false : _favoriteData[prodId],
          imageUrl: prodData['imageUrl'],
        ));
      });
      _items = loadedProducts;
      notifyListeners();
    } catch (error) {
      print(error);
      throw (error);
    }
  }

  

I am getting this error

flutter: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'bool' [VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'bool'

But can't find any solutions to fix this I tried my best to fix but I can't

CodePudding user response:

The error is on

isFavorite: _favoriteData == null ? false : _favoriteData[prodId],

isFavorite is required to be a bool and you try to asssign _favoriteData[prodId] to it, which is a map.

I believe you just want isFavorite to be true when it exist, so just change it to

isFavorite: _favoriteData[prodId] != null,
  • Related