skip to Main Content
final alertsScreen = AlertsScreen(userId: loginController.currentUser.value?.id ?? '');
    Navigator.push(context, MaterialPageRoute(builder: (context) => alertsScreen));
    Future<bool> _onWillPop() async {
    // Reload data when the user presses the back button
    await fetchNotifications;
    await markNotificationAsRead;
    await _showNotificationDetails;
    print("hello");
    Navigator.pop(context, alerts.length);
    return true;
    // Allow the back navigation
  }

i use this fuction in back screen go but not page refresh and not update notificatoion count

3

Answers


  1. The problem is that you are using Navigator.pop() to return to the previous page. This will simply pop the previous page off the navigation stack, without reloading it.

    To reload the previous page, you can use Navigator.pushReplacement() instead. This will push a new instance of the previous page onto the navigation stack, effectively replacing the existing instance.

    final alertsScreen = AlertsScreen(userId: loginController.currentUser.value?.id ?? '');
        Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => alertsScreen));
        Future<bool> _onWillPop() async {
        // Reload data when the user presses the back button
        await fetchNotifications;
        await markNotificationAsRead;
        await _showNotificationDetails;
        print("hello");
        Navigator.pop(context, alerts.length);
        return true;
        // Allow the back navigation
      }
    
    Login or Signup to reply.
  2. If you insist on using Navigator.pop(), you have to consider using state management. State management will take care of the rebuilding of widgets that watches any changes in an object (in your case, an int notificationCount), reflecting the most updated value of the object.

    Additional Note:
    There are many reliable state management packages on offer in pub.dev. You can choose one that you are most comfortable with. I personally use riverpod.

    An example for your case using riverpod.

    Add the Riverpod package to your pubspec.yaml file.

    dependencies:
      flutter:
        sdk: flutter  
      flutter_riverpod: ^2.3.6
    

    Create a file, e.g. lib/tool/provider/notificationCount.dart and create notifier provider for the object you want to keep track of, in this case is the notificationCount int.

    Since it is a simple integer type object, using State Provider should be ample.

    final notificationCountProvider = StateProvider<int>((ref) {
      // initial value to return (e.g. 0)
      return 0;
    });
    

    to update the value on following an event (for example a button click), you can use ConsumerWidget as follow,

    // extends ConsumerWidget instead of StatelessWidget
    class FirstScreen extends ConsumerWidget {
      @override
      // now we have access to WidgetRef
      Widget build(BuildContext context, WidgetRef ref) {
        // watch the notificationCount
        final notificationCount = ref.watch(notificationCountProvider);
        return Scaffold(
          body: Text(notificationCount),
        );
      }
    }
    

    Then in your second screen, where you put the event logic that changes the notification count,

    class SecondScreen extends ConsumerWidget {
          @override
          Widget build(BuildContext context, WidgetRef ref) {
            return Scaffold(
              body: ElevatedButton(
                child: Text('do something, adds the count, and go back'),
                onPressed: () async {
                     // asynchronous await for a future function to complete
                     await exampleFutureFunction();
                     // increase the count by 1
                     ref.read(notificationCount.notifier).state++;
                     // pop the second screen to return to first 
                     Navigator.pop(...);
                },
              ),
            );
          }
        }
    

    When SecondScreen pop to FirstScreen, the notificationCount, will reflect the latest value.

    Using state management is highly recommended as it will make your code more robust, and easier to maintain as your code base grows.

    Here’s a very good resource to learn about riverpod.

    Login or Signup to reply.
  3. You could return any value that you need from pop action.
    
    ...
    await fetchNotifications;
    await markNotificationAsRead;
    await _showNotificationDetails;
    ....
    Navigator.pop(context, returnValue);
    
    -> returnValue can be anything
    
    A map, A model, A variable type etc.
    
    And check your returnValue after pop action ->  
    
    final returnValue = await Navigator.push(...);
    
    if (returnValue != null) {
       setState(() {
          // do anything that you want after pop action
       });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search