1

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.

shanmkha
  • 416
  • 1
  • 8
  • 27

4 Answers4

0

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(),
        ),
      ),
    );
  }
          ),
        ),
      ),
    );
  }
}
Saral Karki
  • 4,530
  • 2
  • 7
  • 10
0

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(),
            );
          }),
        ),
AG007
  • 1
  • 1
0

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

IBRAHIM ALI MUSAH
  • 831
  • 10
  • 19
0

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,
        ),
      ),
    );
  }