I get the error: Could not find the correct Provider<User> above this settings widget (ALREADY FIXED)

Issue

FIXED:
I didn’t have a streamprovider set before materialapp

I have this:

 final user = Provider.of<User>(context);

return StreamBuilder<UserData>(
  stream: DatabaseService(uid: user.uid).userData,
  builder: (context, snapshot) {
    if(snapshot.hasData){

      UserData userData = snapshot.data;

I have a class of user:

class User {

final String uid;
final String email;
final String instagram;
String age;
String gender;
bool account;

User({this.email, this.age, this.gender, this.uid, this.instagram, this.account,});

}

I don’t know what the problem is, i have used it somewere else and there it is working fine

here the error log:

Error: Could not find the correct Provider<User> above this Settings Widget

To fix, please:

  • Ensure the Provider is an ancestor to this Settings Widget
  • Provide types to Provider
  • Provide types to Consumer
  • Provide types to Provider.of()
  • Ensure the correct context is being used.

Solution

Make sure to have Provider above the Settings widget in the widget tree for this you can pass User class from the provider by wrapping the whole material app with Provider() or just wrap the Settings widget with Provider

Provider<User>(
create:(context)=>User(),
child:MaterialApp(
  //app
 ),
),

or

Provider<User>(
    create:(context)=>User(),
    child:SettingsWidget(
      //app
     ),
    ),

Answered By – Khadga shrestha

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *