【问题标题】:Flutter>Hive: type 'Null' is not a subtype of type 'bool'Flutter>Hive:“Null”类型不是“bool”类型的子类型
【发布时间】:2021-07-28 15:04:46
【问题描述】:

在我的应用程序中,我想在启动画面中检测此应用程序是否首次启动。 为此,我想使用 hive nosql 包。

之后,如果应用程序第一次启动,它将打开欢迎页面,如果不是登录页面。

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

我调用函数“var _isFirstTime = Settings().isFirstTime;”它应该返回一个布尔值

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

我收到了这个错误:

════════ 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'

我该如何解决这个问题?稍后我也想将设置服务用于其他设置...

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    在setting_service.dart中, 我认为这一行 -> _settingsStorage.get('firstRun'),正在返回 null。据我了解,您应该做的是,每当您将 firstRun 设置为 null 时,都应将其分配为 true。

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

    【讨论】:

    • 这对我有用。我正在提取布尔值,但该值不存在,因此返回 null。 ?? false 修复了它。
    猜你喜欢
    • 2021-10-22
    • 2022-11-12
    • 1970-01-01
    • 2022-11-18
    • 2023-01-26
    • 2019-12-05
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多