Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's the correct way to use Hive for app theming (light/dark mode)? #54

Closed
ninest opened this issue Sep 19, 2019 · 3 comments
Closed
Labels
question Further information is requested

Comments

@ninest
Copy link

ninest commented Sep 19, 2019

Question
What's the correct way to use Hive for app theming (light/dark mode)?

Code sample

class Application extends StatelessWidget {
  Future _openBoxes() async {
    var dir = await getApplicationDocumentsDirectory();
    Hive.init(dir.path);

    return Future.wait([
      Hive.openBox('settings'),
      Hive.openBox('favorites'),
    ]);
  }

  Future _getTheme(BuildContext context) async {
    var settingsBox = Hive.box('settings');

    var theme = settingsBox.get('theme') ?? 'light';
    print("Theme from box: $theme");
    var materialTheme;

    switch (theme) {
      case 'light': {
        materialTheme = ThemesStyles.light(context);
        break;
      }
      case 'dark': {
        materialTheme = ThemesStyles.dark(context);
        break;
      }
      case 'black': {
        materialTheme = ThemesStyles.black(context);
        break;
      }
      default: materialTheme = ThemesStyles.light(context);
    }

    return materialTheme;
  }

  @override
  Widget build(BuildContext context) {
    final ThemeProvider themeProvider = Provider.of<ThemeProvider>(context);

    return FutureBuilder(
      future: _openBoxes(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        return FutureBuilder(
          future: _getTheme(context),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            return MaterialApp(
              debugShowCheckedModeBanner: false,
              theme: snapshot.hasData ? snapshot.data : ThemesStyles.black(context),
              routes: routes(context),
              home: ScrollConfiguration(
                behavior: BounceScrollBehavior(),
                child: HomePage(),
              )
            );
          }
        );
      }
    );
  }
}

Link to file: https://github.com/holt-soundboard/holt-soundboard-mobile/blob/master/lib/main.dart

As seen, I have nested FutureBuilders(), one for opening boxes, one for getting the theme, which I thought would work. However, this doesn't work in production (release app).

Version

  • Platform: Android, Mac
  • Hive version: 1.0.0
@ninest ninest added the question Further information is requested label Sep 19, 2019
@simc
Copy link
Member

simc commented Sep 19, 2019

Your _getTheme() method does not need to be async. This already removes one FutureBuilder.

What error are you seeing?

@ninest
Copy link
Author

ninest commented Sep 19, 2019

@leisim
Thank you for your quick reply, I will try this!

And I'm sorry to cause you inconvenience, but the error was actually on my side (Gradle errors) after downloading the app from the PlayStore. Hive has worked amazing!

@ninest ninest closed this as completed Sep 19, 2019
@simc
Copy link
Member

simc commented Sep 19, 2019

Glad you like it. Don't hesitate to ask if you need further assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants