'How to show snackbar on "HomePage" when navigating back from other pages

I'm implementing functionality to refresh the homepage when navigating from other pages so I want to display a snack bar on the homepage to show that the home page is refreshed, and the display on the homepage can also be updated: I have tried the code below please check it out

@override
  void initState() {
    super.initState();

    walletID = int.parse(UserPreferences.getWalletID() ?? '');
    customerID = int.parse(UserPreferences.getCustomerID() ?? '');
  }

  void refreshData() {
    callBackId++;
  }

  FutureOr onGoBack(dynamic value) {
    refreshData();
  }

  void navigateToOtherPages() {
    Route route = MaterialPageRoute(builder: (context) => const MyApp());
    Navigator.push(context, route).then((value) => onGoBack(callBackId));
  }

  StokvelListState currentState = StokvelListState.showAllListState;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.secondaryColor,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0.0,
        leading: GestureDetector(
          onTap: () {
            // Navigator.push(context,
            //     MaterialPageRoute(builder: (context) => const HomeScreen()));
            setState(() {
              Navigator.of(context)
                  .push(
                MaterialPageRoute(
                  builder: (context) => const HomeScreen(),
                ),
              )
                  .then((_) {
                setState(() {
                  onGoBack(callBackId);
                  setState(() {
                    final snackBar = SnackBar(
                        content: const Text('Home Screen Refreshed!'),
                        action: SnackBarAction(
                          label: 'Close',
                          onPressed: () {},
                        ));
                    ScaffoldMessenger.of(context).showSnackBar(snackBar);
                  });
                });
              });
            });
          },

I want to show the snack bar at the bottom when navigating back from the pages on the Homepage, please correct me on my code I found on Flutter Website https://docs.flutter.dev/cookbook/design/snackbars#1-create-a-scaffold

This is the back button I want to click and show the snack bar on homepage



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source