ChangeNotifierProvider&&ChangeNotifierProvider.value

Issue

I cant understand the difference between ChangeNotifierProvider and ChangeNotifierProvider.value,
and when use every case
ChangeNotifierProvider :-

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
       create: (context) => ProductsProvider(),

ChangeNotifierProvider.value :-

return GridView.builder(
      padding: const EdgeInsets.all(10.0),
      itemCount: products.length,
      itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
        value: products[i],

Solution

ChangeNotifierProvider:
It creates a new instance of some class from where you want to consume data

ChangeNotifierProvider.value
In case if you already have a instance which has some data loaded previously and you want to consume the previously loaded data then you need to use ChangeNotifierProvider.value

Answered By – Shanto

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.