Dart Flutter Article

ChangeNotifier selector

Article about how to select a value from the ChangeNotifier and transform it to a ValueListenable
Plague Fox 1 min read
ChangeNotifier selector
ChangeNotifier selector

Have you had a situation where you must select and rebuild the interface only to change specific fields of your ChangeNotifier?
Not convenient to use AnimatedBuilder with ChangeNotifier instead of ValueListenableBuilder?

Of course, I had to. So I made a simple extension that converts ChangeNotifier to ValueListenable and has filtering capabilities.

And transformation pipeline looks like this:

ChangeNotifier ==[select & filter]==> ValueListenable<Value>

For example, you can transform your AppModel and rebuild some parts of widgets when localization is changed and only when the language code has changed:

ValueListenableBuilder<Locale>(
  valueListenable: appModel.select<Locale>(
    (cn) => cn.locale,
    (prev, next) => prev.languageCode != next.languageCode
  ),
  builder: (context, locale, child) => Text(locale),
)

Complete code of my extension:

Share
Comments
More from Plague Fox

Plague Fox

Engineer by day, fox by night. Talks about Flutter & Dart.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Plague Fox.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.