Akadeax
Akadeax

Reputation: 132

Flutter Custom NotificationListener not receiving Notifications?

class MyParentWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return NotificationListener<MyCustomNotification>(
        child: MyChildWidget(),
      onNotification: (notification) {
          print("Received Notification!");
          return true;
      },
    );
  }

}

class MyChildWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FlatButton(
      child: Text("Press to send"),
      onPressed: () {
        print("dispatched notification!");
        MyCustomNotification()..dispatch(context);
      },
    );
  }
}

class MyCustomNotification extends Notification {
  const MyCustomNotification();
}

This flutter code results in a GUI that consists of only a clickable button, which dispatches an instance of MyCustomNotification on click. This outputs this to the console:

flutter: dispatched notification!
flutter: Received Notification!

This is the element tree:

...
-> MyParentWidget
   -> NotificationListener<MyCustomNotification>
      -> MyChildWidget
         -> FlatButton
            -> Text

Up until this point, this is all fine and works. As the console shows, the notification is both dispatched and received. However, if I try to combine these widgets, as in the same StatelessWidget both dispatches and receives an event, e.g.

class MyCombinedWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return NotificationListener<MyCustomNotification>(
        child: FlatButton(
          child: Text("Press to send"),
          onPressed: () {
            print("dispatched notification!");
            MyCustomNotification()..dispatch(context);
          },
        ),
      onNotification: (notification) {
          print("Received Notification!");
          return true;
      },
    );
  }
}

(i.e. paste the content of MyChildWidget's build return into where it was instantiated). This no longer works, even though the widget tree is almost exactly the same:

...
-> MyCombinedWidget
   -> NotificationListener<MyCustomNotification>
         -> FlatButton
            -> Text

This should work, right? The notification is still dispatched in a descendant, and caught in an ancestor of it. But unfortunately this is the only console output:

flutter: dispatched notification!

Is there a solution to this, aside from separating every time I want to create Notification Listeners? And even more important, at least for me: Why does this happen? What causes the fact that the notification is no longer caught?

Upvotes: 1

Views: 1616

Answers (1)

Basel Abuhadrous
Basel Abuhadrous

Reputation: 1680

The answer to why this is happening is as mentioned in the docs:

A widget that listens for Notifications bubbling up the tree.

what you are trying to do is that receive the notification at the same level of where it was dispatched.

It works when they are separated because the child has a different context, whereas in the second code snippet they have the same context, so you may wrap the button in builder and that may work since it passes a new context to its children

Upvotes: 3

Related Questions