Passing value in route.isFirst method in Flutter

Viewed 12

I am creating an aquarium tank management app using Flutter. I'd like to show a SnackBar when one of the tank is deleted after confirmed in AlertDialog, but it doesn't work.

In this app, you can see the list of tanks in 1st page. When you tap one of tanks, you go to 2nd page. In 2nd page there is an IconButton you can edit or delete the tank. When you tap "delete", an alert dialog pops up and ask for your confirmation. When you tap "OK", then the item is actually deleted.

I used "route.isFirst" method when going back to 1st page after tapping "OK". However, it appears the method does't pass bool value. I think this is why SnackBar doesn't show in my 1st page. Is there any work-around to pass value when using isFirst method?

1st page:

final List<Widget> widgets = tanks.map((tank) => Card(
child: InkWell(
  onTap: () async {
    final bool? deleted = await Navigator.push(context, MaterialPageRoute(builder: (context) => TankDetailPage(tank: tank)));
    if (deleted != null && deleted) {
      final snackBar = SnackBar(backgroundColor: Colors.green, content: Text('Deleted Tank'));
      ScaffoldMessenger.of(context).showSnackBar(snackBar);
    }
    model.fetchTankList();
  },

2nd page:

CupertinoDialogAction(
        onPressed: () async {
          try {
            model.startLoading();
            await model.deleteData();
            Navigator.of(context).popUntil((route) => route.isFirst);
          } catch (e) {
            if (kDebugMode) {
              print(e);
            }
            final snackBar = SnackBar(content: Text(e.toString()));
            ScaffoldMessenger.of(context).showSnackBar(snackBar);
          } finally {
            model.endLoading();
          }
        },
        child: const Text('OK')
    ),
0 Answers
Related