This is my new library for managing flutter rebuilds based on state changes. It is very similar to AnimatedBuilder but takes a factory to create the notifier instead of an instance of the notifier. It also includes the notifier in the builder method. I would like to know if you can spot potential bugs.

import 'package:flutter/material.dart';

///👷🏽 A Builder for any Listenable notifier/controller
class NotifierBuilder<T extends Listenable> extends StatefulWidget {
  ///Constructs a [NotifierBuilder]
  const NotifierBuilder({
    required this.notifier,
    required this.builder,

  ///🔔The Builder calls this when it needs a new notifier. That will only
  ///happen once
  final T Function() notifier;

  ///🏗️ The Builder calls this when it needs to build the widget tree. You can
  ///access the notifier and its state here
  final Widget Function(
    BuildContext context,
    Widget? child,
    T notifier,
  ) builder;

  ///👶🏽 This is entirely optional. You can use this prebuilt Widget in the
  ///builder to improve performance, but you don't need to
  final Widget? child;

  State<NotifierBuilder> createState() => NotifierBuilderState<T>();

///This exposes the state of the [NotifierBuilder]. You can access the notifier
///in widget tests to verify the state
class NotifierBuilderState<T extends Listenable>
    extends State<NotifierBuilder<T>> {
  ///The notifier that was created by the [NotifierBuilder.notifier] function
  T? notifier;

  void initState() {
    notifier ??= widget.notifier();

  void didChangeDependencies() {
    notifier ??= widget.notifier();

  void dispose() {

  void _handleChange() {
    setState(() {});

  Widget build(BuildContext context) =>
      widget.builder(context, widget.child, notifier!);
  • 3
    \$\begingroup\$ Would anyone care to explain why there is a vote to close on this question? I don't see anything wrong. \$\endgroup\$
    – pacmaninbw
    Mar 4, 2023 at 15:56


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.