Error Related To Flutter Provider Package
I am building a setting widget in my flutter app and I am getting this error : Error: Could not find the correct Provider above this SettingsForm Widget Update Adding whole debug s
Solution 1:
Because you have to declare the provider class above the class were your using it , if u find this ans crt mark it as crt
@overrideWidgetbuild(BuildContext context) {
returnMultiProvider(
providers: [
ChangeNotifierProvider(create: (ctx) =>MyUser(),),
],
child: MaterialApp());
Solution 2:
You have to warp the parent class with the provider class you are using inside. For doing so the easiest way is to add a static method in widget havingMaterialPageRoute
which helps to navigate to SettingsForm
screen.
class SettingsForm extends StatefulWidget {
static Widget getWidget() {
return new Provider(
create: (_) => MyUser(),
child: ChangeNotifierProvider(
create: (BuildContext context) => MyUser(),
builder: (_,_) => SettingsForm()
),
);
}
@override
_SettingsFormState createState() => _SettingsFormState();
}
To open SettingsForm
screen just call getRoute
function on button pressed. Check the below code.
Open SettingsForm screen from Home screen
void _showSettingsPanel() {
showModalBottomSheet(context: context, builder: (context) {
return Container(
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 60.0),
child: SettingsForm.getWidget(), <-- Here
);
});
}
Post a Comment for "Error Related To Flutter Provider Package"