I have a simple app that lists a bunch of items that are saved in Isar DB:
@override
Widget build(BuildContext context) {
var body = Column(
children: <Widget>[
const SizedBox(height: 2.0),
Expanded(
child: FutureBuilder<List<Item>>(
future: getAllItems(),
builder:
(BuildContext context, AsyncSnapshot<List<Item>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (snapshot.hasError) {
// If there was an error while fetching the data, show an error message
return Center(
child: Text('Error: ${snapshot.error}'),
);
} else if (snapshot.hasData) {
var items = snapshot.data!;
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ItemRowCard(
item: items[index],
onRowTap: () async {
// tapping the row takes you to edit the item name
dynamic result = await Navigator.pushNamed(context, '/edit_item', arguments: {
'item_name': item.name,
'item_id': item.id
});
if (result != null) {
setState(){}; // should cause the build function to rerun
}
},
);
},
);
} else {
return const Center(
child: Text('No data found.'),
);
}
},
),
),
],
);
}
The getAllItems()
uses Isar db to fetch data:
Future<List<Item>> getAllFolders() async {
final isar = await db;
final items = await isar.items.where().findAll();
return Future.value(items);
}
This works BUT whenever I add or edit an item (see the onRowTap
call), the page refresh does not show the change unless I insert a delay of 1 second or so before I call setState
after getting the result.
What am I missing? I am at my wits end over this :). All Isar calls are async with await, so techically they should return AFTER the DB operation finishes, and I should be able to call the setState without manually adding delays!