Global bus file eventbus.dart

// The subscriber callback signature typedef void EventCallback(arg); Class EventBus {// Private constructor eventbus._internal (); Static EventBus _singleton = new eventbus._internal (); // Factory constructor factory EventBus()=> _singleton; Var _emap = new Map<Object, List<EventCallback>>(); / / add the subscriber void on (eventName, EventCallback f) {if (eventName = = null | | f = = null) return; _emap[eventName] ?? = new List<EventCallback>(); _emap[eventName].add(f); Void off(eventName, [EventCallback f]) {var list = _emap[eventName]; if (eventName == null || list == null) return; if (f == null) { _emap[eventName] = null; } else { list.remove(f); Void emit(eventName, [arg]) {var list = _emap[eventName]; if (list == null) return; int len = list.length - 1; For (var I = len; i > -1; --i) { list[i](arg); Var bus = new EventBus(); var bus = new EventBus();Copy the code

The global bus is introduced to send messages

bus.emit('login',loginMsg);
Copy the code

Receives the message

Bus. on('login', (arg){print(" $arg"); setState(() { _currentIndex = 1; }); });Copy the code

Adding a Link Description