没有使用Flutter中的配置单元为类型“Object”定义方法“get”

6tr1vspr  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(84)

当我试图使用Hive库缓存WordPress网站的新闻时,我遇到了这种问题,我在代码中遇到了这个问题,方法'get'没有为“box?.get“上的类型'Object'定义。这是我第一次在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(),
          },
        ),
      ),
    );
  }
}
jhdbpxl9

jhdbpxl91#

您是否已注册盒式适配器?

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

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

相关问题