0

I want to notify the wrapper when a value changes from null to a number, but the wrapper seems not to receive the change. Check my code below:

class setID with ChangeNotifier{
String studyID;

void setStudyID(String study){
  studyID = study;
  notifyListeners();
  }
}

in main.dart,

return MultiProvider(
  providers: [
    ChangeNotifierProvider(create: (_) => setID()),
    // others providers here
    ],
  child: MaterialApp(
      home: Wrapper(),
    ),

in wrapper.dart,

final studyID = Provider.of<setID>(context).studyID;

When the value is changed in setID class, no value is received in the wrapper. Could anyone help me out? Thank you in advance.

Winnieeee
  • 43
  • 4

1 Answers1

1

Simply change the code from

providers: [
    ChangeNotifierProvider(create: (_) => setID()),
    // others providers here
    ],

to,

providers: [
    ChangeNotifierProvider<setID>(create: (_) => setID()),
    // others providers here
    ],

And in wrapper get the provider listener as

final studyID = Provider.of<setID>(context);

The issue here is ChangeNotifierProvider doesn't know which variable/class object to monitor. It should do the trick. For more info read the official documentation of Providers

Saurabh Kumar
  • 2,088
  • 14
  • 17