Flutter provider, question around Dart syntax

Issue

I’m relatively new to Dart/Flutter,

Just struggling to understand some code/syntax and wondered if someone can help explain.

Im looking at the example of setting up multiple providers and I cant get my head round the code for setting up the update..

       providers: [
        // In this sample app, CatalogModel never changes, so a simple Provider
        // is sufficient.
        Provider(create: (context) => CatalogModel()),
        // CartModel is implemented as a ChangeNotifier, which calls for the use
        // of ChangeNotifierProvider. Moreover, CartModel depends
        // on CatalogModel, so a ProxyProvider is needed.
        ChangeNotifierProxyProvider<CatalogModel, CartModel>(
          create: (context) => CartModel(),
          update: (context, catalog, cart) {
            cart.catalog = catalog;
            return cart;
          },
        ),
      ],

Specifically…

 update: (context, catalog, cart) {
            cart.catalog = catalog;
            return cart;
          }

I thought it was a function that takes in 3 parameters context, catelog, cart

But I dont see anywhere where they are first instantiated

Can anyone explain what is going on here?

Thanks

Solution

update: denotes a parameter to the ChangeNotifierProxyProvider<CatalogModel, CartModel> constructor, passing it an anonymous function that takes three parameters. The code in (or near) the ChangeNotifierProxyProvider will be invoking this function as necessary.

Answered By – Randal Schwartz

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.