Home > Back-end >  showing console an error while there is no record in firebase data
showing console an error while there is no record in firebase data

Time:08-08

I am fetching order data from firebase, working all well but if there is no data it showing error in console

I have added a if statement if data not available, even thought its not solving

here is my code for fetching order data and something happening with products list if no product in firebase data

so how to solve now if no product

Future<void> fetchandsetorders() async {
    final url = Uri.parse(
        myelin);
    final response = await http.get(url);
    final List<OrderItem> loadedorders = [];
    final extradeddata = json.decode(response.body) as Map<String, dynamic>;
    
//I added this statement but still not solving
if(extradeddata==null)
      return;
    extradeddata.forEach((orderid, orderdata) {
      loadedorders.add(
        OrderItem(
          id: orderid,
          products: (orderdata['products'] as List<dynamic>).map((item) {
            return CartItem(
                id: item['id'],
                title: item['title'],
                quantity: item['qty'],
                price: item['price']);
          }).toList(),
          amount: orderdata['amount'],
          date: DateTime.parse(orderdata['date']),
        ),

      );
    });

    _orders=loadedorders;
    notifyListeners();
  }

showing error is



E/flutter (32144): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'Map<String, dynamic>' in type cast
E/flutter (32144): #0      Orders.fetchandsetorders (package:finalshopapp/orders.dart:31:53)
E/flutter (32144): <asynchronous suspension>

CodePudding user response:

The error you're facing is caused from this line:

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

When response.body is null, you can't cast it to Map<String, dynamic>

So, one solution could be is to check if response.body is null, before casting it to Map. Sample code:

Future<void> fetchandsetorders() async {
    final url = Uri.parse(
        myelin);
    final response = await http.get(url);
    final List<OrderItem> loadedorders = [];
    // Add this line here
    if (response.body == null) return;
    final extradeddata = json.decode(response.body) as Map<String, dynamic>;
    
// NO NEED FOR THIS
//if(extradeddata==null)
      //return;
    extradeddata.forEach((orderid, orderdata) {
      loadedorders.add(
        OrderItem(
          id: orderid,
          products: (orderdata['products'] as List<dynamic>).map((item) {
            return CartItem(
                id: item['id'],
                title: item['title'],
                quantity: item['qty'],
                price: item['price']);
          }).toList(),
          amount: orderdata['amount'],
          date: DateTime.parse(orderdata['date']),
        ),

      );
    });

    _orders=loadedorders;
    notifyListeners();
  }
  • Related