Skip to content

Conversation

@ilopX
Copy link
Collaborator

@ilopX ilopX commented Feb 3, 2022

Observer pattern

Observer is a behavioral design pattern that lets you define a subscription mechanism to notify
multiple objects about any events that happen to the object they’re observing.

Tutorial: here.

Subscriber Flutter Widget example

This is a complex example of an Observer pattern, connected to a Flutter application. The example includes, the previously implemented AppObserver example pattern, which you can see here.

Online demo:

Click on the picture to see the demo.

image

Diagram:

image

Sequence

image

Client code:

void main() {
    // widget that generates a new event
    ElevatedButton(
      child: Text('Generate new hash'),
      onPressed: () {
        final hash = 'some hash';
        observer.notify(NewHashEvent(hash));
      },
    );
    
    // widget event consumer NewHashEvent
    SubscriberWidget<NewHashEvent>(
      observer: observer,
      builder: (context, event) {
        return Text(
          event?.newHash ?? 'Hash no generated' ,
        );
      },
    );
}

@neochief neochief merged commit a212519 into RefactoringGuru:master Feb 4, 2022
@ilopX ilopX deleted the observer-pattern-example-flutter-subscriber-widget branch February 4, 2022 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants