Flutter has nothing to say about Notification, and can be used to pass data from a child to a parent.
However, if you want to realize one-to-many notification with multi-page, cross-page and broadcast, it is similar to NSNotificationCenter in iOS, which has not been found in Fluter.
Features like this:
3 pages, including page3 to send a notification, page1 and page2 to receive the notification, make the corresponding operation. There are many ways to do this, but here’s how to do it with notifications.
Since Flutter does not provide the NSNotificationCenter feature found in iOS, can we do it ourselves?
Ideas:
The first idea that came to mind was to design the notification center into a singleton class for easy use. Secondly, methods such as joining notification, sending notification, and removing notification should be provided.
- Join notification: Record each join notification and use
name
Mark different notifications; - Give notice: according to
name
Get the corresponding notification in the notification center, and implement the notification method; - Remove notification: according to
name
Get the corresponding notification in the notification center and remove the corresponding notification;
The above is only a preliminary idea, there must be some imperfections, we will improve later.
Ideas have, the following began to achieve the code:
Class RCNotificationCenter {/// singleton factory RCNotificationCenter() => _getInstance(); static RCNotificationCenter get instance => _getInstance(); static RCNotificationCenter _instance; RCNotificationCenter._internal(); static RCNotificationCenter _getInstance() { if (_instance == null) { _instance = RCNotificationCenter._internal(); } return _instance; Function(dynamic value)> _poolMap = Map<String, Function(dynamic value)>(); Void addObserver(String postName,notification(dynamic value)) {_poolMap[postName] = notification; } void postNotification(String postName, If (_poolmap.containsKey (postName)) {if (_poolmap.containsKey (postName)) {_poolMap[postName](value); }} void removeNotification(String postName) {if (_poolmap.containskey (postName)) { _poolMap.remove(postName); } // clear the notification center void removeAll(){_poolmap.clear (); }}Copy the code
When using:
RCNotificationCenter().addobServer ("postName",(value){print(" Received notification: $object"); }); RCNotificationCenter(). PostNotification ("postName", value);Copy the code
Question:
There are two main ones:
-
Although the notification function can be realized, because Map is used, each time after joining, the one-to-many with the same name is directly reassigned to overwrite, and the notification cannot be realized. For notifications of the same name, only the last one will be executed.
-
There is a problem with removing notifications. After a notification is added to multiple pages, a notification that is intended to remove only one page will appear. As a result, all notifications with the same name will be removed. Notifications for a single specific page cannot be removed.
Perfect:
In order to solve the above problems, each notification needs to be corresponding to three elements: name, key (specific who joined, similar to iOS self, later can remove the notification on a specific page according to this element, without affecting the same notification on other pages, so that who joined and who removed), notification notification execution method.
To this end, I create a notification model to correspond to each notification.
Improved code:
Class RCNotificationCenter {// singleton factory RCNotificationCenter() => _getInstance(); static RCNotificationCenter get instance => _getInstance(); static RCNotificationCenter _instance; RCNotificationCenter._internal(); static RCNotificationCenter _getInstance() { if (_instance == null) { _instance = RCNotificationCenter._internal(); } return _instance; List<RCNotificationModel> pool = []; Void addObserver(String postName, dynamic key,void notification(dynamic value)) { RCNotificationModel model = RCNotificationModel.fromList([postName,key,notification]); pool.add(model); } void postName (String postName, dynamic value) {// Pool.foreach ((element) {if(element.postname == postName){element.notification(value); }}); } void removeOfName(String postName) {// Thread safety, Pool.removewhere ((element) => element.postName == postName); Void removeOfKey(dynamic key){pool.removeWHERE ((element) => element.key == key); } /// Clear the notification center void removeAll(){pool.clear(); }} class RCNotificationModel{String postName; dynamic key; Function(dynamic value) notification; / / / write a simple constructor RCNotificationModel. FromList List (List) {this. PostName = List. First; this.key = list[1]; this.notification = list.last; }}Copy the code
When using:
RCNotificationCenter().addobServer ("postName",widget,(value){print(" Received notification: $value"); }); RCNotificationCenter(). PostNotification ("postName", value); // remove all notifications with the name postName RCNotificationCenter().removeofName ("postName"); Dispose () calls RCNotificationCenter().removeofKey (widget). Dispose () calls RCNotificationCenter().removeofKey (widget).Copy the code
The above is to realize the notification center similar to THE NSNotificationCenter in iOS, which can basically realize one-to-many notification with multiple pages, cross pages and broadcast. Optimizable part: listen for the key that joins the notification. When the key resource is released, the notification removal method is automatically called. IOS can bind objects to listen for when they are released. Flutter is still learning!
Of course, any better plan, welcome to leave a message!