0

I have a FutureBuilder with multiple futures, how can I which one of the futures has no data so I can display the proper widget. Basically I want to be able to do something like:

if snapshot.data[0] has no data display widgetOne
else if snapshot.data[1] has no data display widgetTwo
else if snapshot.data[2] has no data display widgetThree

I tried snapshot.data[0].toString().isEmpty == true, snapshot.data[0] == null. Either of those throws

'[]'
js_primitives.dart:30 Dynamic call of null.
js_primitives.dart:30 Receiver: null
js_primitives.dart:30 Arguments: [0]

Using !snapshot.hasData tells me there's no data in one of the future but I want to know which one specifically so I can return the proper widget.

My actual code:

     FutureBuilder(
        future: Future.wait([
          FirestoreService().getUser(widget.username),
          FirestoreService().getUserInventory(widget.username),
          FirebaseRTDB().getAllItems()
        ]),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const AwaitingResultWidget();
          } else if (snapshot.hasData) {
            // Account data
            final AccountModel user = snapshot.data[0];
            // Inventory List
            final List<InventoryItem> inventoryList = snapshot.data[1];
            // Market Data
            final Map<String, Item> itemMap = snapshot.data[2];

            return Column(
              children: [
                Column(
                  children: [
                    kIsWeb ? webUserHeader(user) : androidUserHeader(user),
                  ],
                ),
                Center(
                  child: SingleChildScrollView(
                    child: Column(
                      children: [
                        Text('foo'),
                      ],
                    ),
                  ),
                )
              ],
            );
          } else if (!snapshot.hasData) {
            if (snapshot.data[0] != null) {
              return Container(
                child: Text('foo1'),
              );
            }
            return Container(
              child: Text('foo'),
            );
          } else if (snapshot.hasError) {
            print(snapshot.error);
            return const SomethingWentWrongWidget();
          } else {
            return const UserNotFound();
          }
        },
      ),
Trax
  • 1,445
  • 5
  • 19
  • 39
  • Does this help? https://stackoverflow.com/a/57998509/12349734 – MendelG Jun 28 '22 at 18:26
  • No, !snapshot.hasData works fine but all I am able to do with that is know that one of the futures has no data but not which one. – Trax Jun 28 '22 at 18:27

1 Answers1

0

You can add futures to variables and check its data like

Future<String> firstFuture = FirestoreService().getUser(widget.username);
Future<int> secondFuture = FirestoreService().getUserInventory(widget.username);
FutureBuilder(
  future: Future.wait([firstFuture, secondFuture]),
  builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {
    snapshot.data[0]; //first future
    snapshot.data[1]; //second future
  },
);
Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
  • Tried that, if one of the futures has no data it's caught by `hasError` when trying to read data[] with `Bad state: No element` error. – Trax Jun 28 '22 at 18:42
  • What if you remove that condition check? – Kaushik Chandru Jun 28 '22 at 19:14
  • Then the app will crash when there's an error. What I need is just a way to check if `snapshot.data[0]` has any data or not. – Trax Jun 28 '22 at 19:18
  • In your service if there is an error instead of throwing an error can you return empty string or empty list? By adding a try catch maybe. Then check if its empty here – Kaushik Chandru Jun 28 '22 at 19:28
  • I'm curious. How exactly does the future "have no data"? If the future resolves and has no error, then the data must contain something, even if it's `null`, right...?? – Kwame Opare Asiedu Jun 29 '22 at 01:37
  • Yeah but in this case there is an error in one of the futures – Kaushik Chandru Jun 29 '22 at 01:41