Using Selector for a StreamProvider along with FutureProvider - avoid Null until future returns successfully?

23 Views Asked by At

I'm using a StreamProvider and FutureProvider in a StatefulWidget in my flutter app like -

class _ScheduledDeliveryState extends State<ScheduledDelivery> {
  List<Product>? productList;
  @override
  Widget build(BuildContext context) {

    productList = context.watch<List<Product>>(); // <--- Listening to FutureProvider here

    return Selector<List<Jobs>, List<Map<String, dynamic>>>(
             selector: (BuildContext context, List<Jobs> jobs) =>
              detailsByDeliveryDate(jobs, productList ?? []),
             builder: (BuildContext context, List<Map<String, dynamic>> toBeDeliveredJobs, _) {
  return MyWidget();
}

  List<Map<String, dynamic>> detailsByDeliveryDate(
      List<Jobs> jobs, List<Product> productList) {
    List<Map<String, dynamic>> details = [];
    // Calculations here using jobs and productList to get a list
    return details;
}

In the emulator console, firstly I get an exception: type 'String' is not a sub-type of type 'num'. I understand it comes until I do not get any output from my FutureProvider, as soon as I get it shows the list I need.

What can be done here to avoid it and what is the best practice. (Keeping firebase costs less is also a goal)

Below is how my StreamProvider is created. Similarly, FutureProvider is created (not shown here).

 class MyApp extends StatefulWidget {
      @override
      State<MyApp> createState() => _MyAppState();
    }
    


  class _MyAppState extends State<MyApp> {
        
      @override
      Widget build (BuildContext context) {
      return MultiProvider(
          providers: [
            StreamProvider<List<Jobs>>(create: (_) => JobsProvider.readJobs(), initialData: []) // Listens to a stream from firestore
          ],
          child: MaterialApp.router( 
    //..... some code
        )
       )
      }
0

There are 0 best solutions below