Flutter>Hive: type 'Null' is not a subtype of type 'bool'

Viewed 6495

in my app i want to detect in the splashscreen if this app is started for the first time. For that i want to use the hive nosql package.

After that if the app is started for the first time it will open the welcome page and if not the login page.

main.dart

import 'package:flutter_config/flutter_config.dart';
import 'package:flutter/material.dart';
import 'package:app/pages/splash/splash_page.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'config/theme/theme.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterConfig.loadEnvVariables();
  await Hive.initFlutter();
  await Hive.openBox('settings');
  runApp(const App());
}

class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  void dispose() async {
    Hive.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App',
      debugShowCheckedModeBanner: false,
      theme: lightThemeData(context),
      darkTheme: darkThemeData(context),
      home: const SplashPage(),
    );
  }
}

splash_page.dart

import 'package:flutter/material.dart';
import 'package:app/pages/login/login_page.dart';
import 'package:app/pages/welcome/welchome_page.dart';
import 'package:app/services/settings_service.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({Key? key}) : super(key: key);

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  Widget build(BuildContext context) {
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(
        builder: (_) =>
            Settings().isFirstTime ? const WelcomePage() : const LoginPage(),
      ),
    );

    return const Scaffold(
      body: Center(
        child: SizedBox(
          width: 125,
          height: 125,
          child: Icon(Icons.clear),
        ),
      ),
    );
  }
}

there i call the function "var _isFirstTime = Settings().isFirstTime;" it should return me a bool

settings_service.dart

import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';

class Settings {
  final Box _settingsStorage = Hive.box('settings');

  get isFirstTime {
    if (_settingsStorage.get('firstRun')) {
      return true;
    } else {
      _settingsStorage.put('firstRun', true);
    }
    return false;
  }
}

i got this error:

════════ Exception caught by widgets library ═══════════════════════════════════
The following _TypeError was thrown building SplashPage(dirty, state: _SplashPageState#261ae):
type 'Null' is not a subtype of type 'bool'

how can i solve this? later i would like to use the settings service for other settings as well ...

1 Answers

In the setting_service.dart, I think this line -> _settingsStorage.get('firstRun'), is returning null. From what I understand what you should do is that whenever you get firstRun as null, you should assign it true.

 if (_settingsStorage.get('firstRun') ?? true) {
      return _settingsStorage.get('firstRun') ?? true;
    }
Related