Is there a correct way to listen to a stream / scoped model from a 'root' page or location in a Flutter app and upon receiving some data display an appropriate notification (e.g. Snackbar) on any currently open page (may not be the home page)?
I'd like to be able to display certain notifications across the entire application and not just on a page by page basis.
Here I have a home page that's my apps initial page, it starts listening to a stream from a scoped_model retrieved from context and whenever data is received displays a dialog regardless of the page the user is visiting.
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
MyService service = ScopedModel.of<MyService>(context);
service.events.stream.listen((data) {
showDialog<String>(
context: context,
barrierDismissible: true, // dialog is dismissible with a tap on the barrier
builder: (BuildContext context) {
return AlertDialog(
title: Text('An Event has Occurred'),
content: Text('$data'));});
});
return Scaffold(
//Navigator Push Routes - Page 1 / Page 2 / Page 3 / etc
);}
This kind of works but really doesn't feel correct - has anyone done this before who could offer a better solution?
I could add a similar listener to every page but again that seems really unnecessary