3

main.dart file ChangeNotifierProxyProvider having issues builder method is not defined.

 ChangeNotifierProxyProvider<Auth, Orders>(
      builder: (ctx, auth, previousOrders) => Orders(
        auth.token,
        auth.userId,
        previousOrders == null ? [] : previousOrders.orders,
      ),
    ),

4 Answers 4

6
ChangeNotifierProxyProvider<Auth, Products>(
      create: (_) => Products('', '', []),
      update: (_, auth, prevProducts) {
        return Products(
          auth.token,
          auth.userId,
          prevProducts == null ? [] : prevProducts.items,
        );
      },
    ),
1

Their is no argument like builder in ChangeNotifierProxyProvider, that’s why you are getting that error.

In ChangeNotifierProxyProvider you have to provide create, update and child.

Here, in create you can create your object and in update you can specify when to change provider's value, when notifier depends on some other model.

ChangeNotifierProxyProvider<MyModel, MyChangeNotifier>(
   create: (_) => MyChangeNotifier(),
   update: (_, myModel, myNotifier) => myNotifier
       ..update(myModel),
    child: ...
);
0
1

ChangeNotifierProvider supported builder parameter but migration from v3.x.0 to v4.0.0+ some parameters are changed and builder is one of them.

Instead of that the initialBuilder should be replaced by create.

builder of "proxy" providers should be replaced by update

builder of classical providers should be replaced by create.

0

with provider: ^5.0.0 this also should work as expected

ChangeNotifierProxyProvider<Auth, Products>(
  create: (ctx) {
    return Products();
  },
  update: (ctx, auth, prev) {
    return Products()
      ..setAuthToken(auth.token ?? '')
      ..setItems(prev?.items ?? []);
  },
),

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.