Flutter Provider.of<MyProvider>(…) returns null

Issue

I have a problem with ChangeNotifierProxyProvider.

I have a list of providers like

 providers: [
        ChangeNotifierProvider.value(
          value: Auth(),
        ),
        ChangeNotifierProxyProvider<Auth, MyProvider>(
            create: (context) => MyProvider(),
            update: (ctx, auth, myProvider) => myProvider.update(auth)),
      ],

If I call Provider.of<MyProvider>(context, listen: false); or context.read<MyProvider>(); in my classes I got a null object.

In another case, if I replace ChangeNotifierProxyProvider to ChangeNotifierProvider (without proxy), I got my provider object.

I didn’t find any similar questions. So I guess I miss something.

Solution

The syntax is:

ChangeNotifierProxyProvider<Auth, MyProvider>(
   create: (context) => MyProvider(),
  update: (ctx, auth, myProvider) {
    myProvider.update(auth);
    return myProvider;
  }
) 

Answered By – Rémi Rousselet

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.