[英]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.