0

I want to fetch all orders based on the value of date variable.

Here's the function I am using to fetch:

Future<void> fetchAllOrders() async {
    var now = DateTime.now();
    var today = now.day.toString()+now.month.toString()+now.year.toString();
    print('Todays time is ' + today.toString()); 
    var filterString ='orderBy="date"&equalTo="$today"';
    final url =     'https://tm-9120.firebaseio.com/ordersdev.json?auth=$authToken&$filterString';
    final response = await http.get(Uri.parse(url));
    final data = json.decode(response.body) as Map<String, dynamic>;
    print(data); 
}

Here's the structure of my realtime database table:

ordersdev -> userId -> order object containing date variable

enter image description here

The function returns null. What am I missing?

mcfred
  • 1,183
  • 2
  • 29
  • 68

1 Answers1

1

You can't filter on a nested list. You would need to change the path https://tm-9120.firebaseio.com/ordersdev.json to semething like this https://tm-9120.firebaseio.com/ordersdev/firstListUID.json to go to one of the nested elements and then you can filter to the date.

The filter restriction to the one level is very good explained here. It doesn't matter if you use the REST API or SDK (the restriction is the same)

Tarik Huber
  • 7,061
  • 2
  • 12
  • 18
  • Fetching the whole list from DB and filtering on client side will be very slow as the list grows. Ain't any other way of doing this server side? – mcfred Aug 03 '21 at 10:16
  • 1
    The other way to do that would be to change your data model to allow the query, as I described in the linked answer. – Frank van Puffelen Aug 03 '21 at 14:20
  • I never wrote that you should download the whole list. Just mentioned that it's not possible to filter on nested data. The best solution would be to restructure your database structure. – Tarik Huber Aug 03 '21 at 20:42