24

I have this code in my main.dart:

main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(builder: (context) => Auth()), // /**problem here. builder displayed with strikethrough line**/
      ],
      child: App(),
    ),
  );
}

since about 2 days ago, my visual studio code showing this warning:

enter image description here

so I guess builder parameter on ChangeNotifierProvider is deprecated. I searched everywhere but can't find alternative to this builder parameter. So how to remove these warning? Below is my flutter version using flutter --version command on Windows 10

> flutter --version
Flutter 1.9.1+hotfix.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 68587a0916 (3 months ago) • 2019-09-13 19:46:58 -0700
Engine • revision b863200c37
Tools • Dart 2.5.0

For any help, thanks in advance

Dika
  • 2,213
  • 4
  • 33
  • 49

5 Answers5

58

Since provider version 3.2.0 "builder" is marked as deprecated in favor of "create".

More info can be found in the change log

So should do:

ChangeNotifierProvider(create: (context) => Auth())
Adelina
  • 10,915
  • 1
  • 38
  • 46
12

you can pass with create as builder.

    return Provider<MyProvider>(
        create: (context) => MyProvider(

        ),
        child: HomePage(),
    );
Augustin R
  • 7,089
  • 3
  • 26
  • 54
Khadga shrestha
  • 1,120
  • 6
  • 11
  • thank you for your answer. but I choose @Nuts' answer because his description is clearer – Dika Nov 29 '19 at 09:03
6

you can use "create" instead of "builder"

main() {
 runApp(
   MultiProvider(
     providers: [
       ChangeNotifierProvider(create: (context) => Auth()), // /**problem here. 
builder displayed with strikethrough line**/
     ],
     child: App(),
   ),
 );

}

Reza Mojed
  • 657
  • 1
  • 7
  • 16
  • 1
    please delete your answer, because your answer not adding anything to existing answers. @Tabrizapps – Dika Dec 08 '19 at 13:15
2

Instead of builder parameter you can use create param with Product() or specific product at Index.

itemBuilder: (ctx, i) => ChangeNotifierProvider(
        create: (context) => products[i],
        child:  ProductItem(),
      ), 

Or can be use as Value version:

itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
        value: products[i],
        child:  ProductItem(),
      ),
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68
2

You need to use create instead of build because of deprecation.

Wrong code example:

enter image description here

True code example:

enter image description here

ChrisF
  • 134,786
  • 31
  • 255
  • 325
canerkaseler
  • 6,204
  • 45
  • 38