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();
}
},
),