'The method 'of' isn't defined for the type 'ThemeProvider'. in Flutter

I am Beginner to Flutter just following the YouTube tutorial on building User Profile while doing everything is OK but Theme Provider gave me the error I didn't understand What's going on can You guys help me, the error is The method 'of' isn't defined for the type 'ThemeProvider', I am getting error at

@override
  Widget build(BuildContext context) {
    final user = UserPreferences.getUser();
    return ThemeProvider(
      initTheme: user.isDarkMode ?  MyThemes.darkTheme : MyThemes.lightTheme,
      child: Builder(
        builder: (context) => MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeProvider.of(context),
          title: title,
          home:  const ProfilePage(),
        ),
      ),
    );
  }

and full code is so long so I Attaching github link for reference, Thanking You Advance. ThemeProvider error

After updating theme_provider in dependency I got 2 more errors below I attached a theme_provider errorscreenshot of it.



Solution 1:[1]

It is themeOf not of. You can check the official docs here

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final user = UserPreferences.getUser();
    return ThemeProvider(
      initTheme: user.isDarkMode ?  MyThemes.darkTheme : MyThemes.lightTheme,
      child: Builder(
        builder: (context) => MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeProvider.themeOf(context),
          title: title,
          home:  const ProfilePage(),
        ),
      ),
    );
  }
          ),
        ),
      ),
    );
  }
}

Solution 2:[2]

Don't skip reading Documentation.Keep on mind your Module docs.

This is the sample of your package owner.Be careful check.

    ThemeProvider(
          initTheme: initTheme,
   ---->  builder: (context, myTheme) {
            return MaterialApp(
              title: 'Flutter Demo',
        ----> theme: myTheme,
              home: MyHomePage(),
            );
          }),
        ),

Solution 3:[3]

i believe its been change to

ThemeModelInheritedNotifier.of(context).theme.brightness

you can use a builder in you material app and pass theme throught it as done in the documentaion here

Solution 4:[4]

I resolved it after hours trying... Uff..

Lets go, I don't know what happened with this package, but have an error.

But for our happiness, I resolved it with a little trap... rsrs

We need tell to "theme: ," a context of ThemeProvider, but the library is bugged.

Then, I tried it.

tell to theme: same code that initTheme.

theme: user.isDarkMode ? MyThemes.darkTheme : MyThemes.lightTheme,

is worth.

put the same code that "initTheme: xxxxx," in "theme: xxxxx".

Worthed for me.

@override
  Widget build(BuildContext context) {
    const user = UserPreferences.myUser;
    return ThemeProvider(
      initTheme: user.isDarkMode ? MyThemes.darkTheme : MyThemes.lightTheme,
      child: Builder(
        builder: (context) => MaterialApp(
          title: 'MyTitle',
          theme: user.isDarkMode ? MyThemes.darkTheme : MyThemes.lightTheme,
          scrollBehavior: SBehavior(),
          home: const SplashArt(),
          debugShowCheckedModeBanner: false,
        ),
      ),
    );
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saral Karki
Solution 2 AG007
Solution 3 IBRAHIM ALI MUSAH
Solution 4 OAlchemista