简体   繁体   English

在 Flutter 中使用 Hive 未为类型“Object”定义方法“get”

[英]The method 'get' isn't defined for the type 'Object' using Hive in Flutter

I am facing this kind of issue while I'm trying to cache News from WordPress website by using Hive library, I'm getting this issue in code, The method 'get' isn't defined for the type 'Object' on " box?.get ".当我尝试使用 Hive 库从 WordPress 网站缓存新闻时,我遇到了这种问题,我在代码中遇到了这个问题,“get”方法没有为“框”上的“对象”类型定义?。得到 ”。 It is the first time I use Hive on Flutter How can I solve this issue?这是我第一次在 Flutter 上使用 Hive 如何解决这个问题?

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

const String SETTINGS_BOX = "settings";
const String API_BOX = "api_data";
///
Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      statusBarColor: Color(0xFF0D47A1),
     
    ),
  );
  setupLocator();

  await Hive.initFlutter();
  await Hive.openBox(SETTINGS_BOX);
  await Hive.openBox(API_BOX);

  runApp(MyApp());
}

/// ignore: must_be_immutable
class MyApp extends StatelessWidget {
  late final List<Article> posts;
  var data;

  @override
  Widget build(BuildContext context) {
    print(Hive.box(SETTINGS_BOX).get("welcome_shown"));
    return ValueListenableBuilder(
      valueListenable: Hive.box(SETTINGS_BOX).listenable(),
      builder: (context, box, child) => box?.get('welcome_shown', defaultValue: false)
          ? HomePage()
          : MultiProvider(
        providers: [
          Provider(create: (context) => FavoriteListModel()),
          ChangeNotifierProxyProvider<FavoriteListModel, FavoritePageModel>(
            create: (context) => FavoritePageModel(),
            update: (context, favoritelist, favoritepage) {
              if (favoritepage == null) {
                throw ArgumentError.notNull('favoritepage');
              }
              favoritepage.favoritelist = favoritelist;
              return favoritepage;
            },
          ),
        ],
        child: MaterialApp(
          initialRoute: "/splashScreen",
          routes: {
            "/splashScreen": (_) => SplashScreen(),
            "/presentationScreens": (_) => OnboardingScreens(),
            "/homepage": (_) => HomePage(),
          },
        ),
      ),
    );
  }
}

Have you registered the box adapter?您是否注册了盒子适配器?

await Hive.initFlutter();
Hive.registerAdapter(<YOUR ADAPTER>);

https://pub.dev/packages/hive/example https://pub.dev/packages/hive/example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:没有为“Box”类型定义“可听”方法(使用 Hive) - Flutter: The method 'listenable' isn't defined for the type 'Box' (using Hive) Flutter Hive:没有为“对象”类型定义吸气剂“值” - Flutter Hive: The getter 'values' isn't defined for the type 'Object' Flutter,未为类型“Object”定义方法“containsKey” - Flutter, The method 'containsKey' isn't defined for the type 'Object' flutter 中没有为类型“Object”定义方法“firstWhere” - The method 'firstWhere' isn't defined for the type 'Object' in flutter 没有为类型“AppState”定义方法“get”。 与 Flutter 合照 - The method 'get' isn't defined for the type 'AppState'. pics with Flutter 为什么我在 hive flutter 中收到“没有为类型 'Object' 定义 getter 'values'”错误? - Why i am getting "The getter 'values' isn't defined for the type 'Object'" error in hive flutter? 方法“of”没有为类型“ThemeProvider”定义。 在 Flutter - The method 'of' isn't defined for the type 'ThemeProvider'. in Flutter Flutter 方法 'then' 没有为类型 'User' 定义 - Flutter The method 'then' isn't defined for the type 'User' FLUTTER 未为“对象”类型定义运算符“[]” - FLUTTER The operator '[]' isn't defined for the type 'Object' 运算符“[]”未为类型“对象”定义 Flutter - The operator '[]' isn't defined for the type 'Object' with Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM