Hive for flutter 仅返回实例而不是实际值
我决定使用 hive 作为我的设置/首选项存储。但是,我无法正确实现我的 Storage
类,因为 getValue
方法始终返回 'Future
实例,而不是实际值。有谁知道如何解决这个问题?
我的 Storage
类仅包含 getValue
和 setValue
,它们始终打开配置单元框,然后应该设置或获取值。此外,我还创建了枚举 StorageKeys
以便拥有一组键并确保我获取或设置专用键的值。
main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
routes: {
"/": (context) => const Home(),
},
));
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
get() async {
return await Storage.getValue(StorageKeys.authTokenKey);
}
void set() async {
await Storage.setValue(StorageKeys.authTokenKey, 'TestValue');
}
@override
Widget build(BuildContext context) {
set();
print(get());
return Scaffold(
backgroundColor: Colors.white,
appBar: ChevronNavigation(),
body: Container(),
);
}
}
存储.dart
class Storage {
static const preferencesBox = '_storageBox';
static Future<void> setValue(StorageKeys key, dynamic value) async {
final storage = await Hive.openBox<dynamic>(preferencesBox);
storage.put(key.toString(), value);
}
static dynamic getValue(StorageKeys key) async {
final storage = await Hive.openBox<dynamic>(preferencesBox);
return await storage.get(key.toString(), defaultValue: null) as dynamic;
}
}
enum StorageKeys {
authTokenKey,
}
I have decided to go with hive as my settings/preference storage. However, I am not able to implement my Storage
class correctly because the getValue
method always returns Instance of 'Future<dynamic>'
instead of the actual value. Does anyone know how to fix that?
My Storage
class just contains the getValue
and setValue
which always opens the hive box and then either should set or get the value. Also, I have created the enum StorageKeys
in order to have a set of keys and make sure I get or set the value to the deticated key.
main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
routes: {
"/": (context) => const Home(),
},
));
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
get() async {
return await Storage.getValue(StorageKeys.authTokenKey);
}
void set() async {
await Storage.setValue(StorageKeys.authTokenKey, 'TestValue');
}
@override
Widget build(BuildContext context) {
set();
print(get());
return Scaffold(
backgroundColor: Colors.white,
appBar: ChevronNavigation(),
body: Container(),
);
}
}
storage.dart
class Storage {
static const preferencesBox = '_storageBox';
static Future<void> setValue(StorageKeys key, dynamic value) async {
final storage = await Hive.openBox<dynamic>(preferencesBox);
storage.put(key.toString(), value);
}
static dynamic getValue(StorageKeys key) async {
final storage = await Hive.openBox<dynamic>(preferencesBox);
return await storage.get(key.toString(), defaultValue: null) as dynamic;
}
}
enum StorageKeys {
authTokenKey,
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
print(get());
将为您提供Future
实例,因为get()
返回一个 Future 对象。解决方案:
您需要在 Future 方法中的
get()
之前编写await
来等待 Future 对象中的实际值。像这样:
在上面的问题中,这不能工作,因为构建方法不能是异步的。您可以将
print(await get())
放在单独的方法中,并将其放在initState
中。像这样:
print(get());
will give youInstance of Future<dynamic>
sinceget()
returns a Future object.SOLUTION:
You need to await the actual value in the Future object by writing
await
beforeget()
in a Future method.Like this:
In your question above, this cannot work as the build method cannot be async. You can put the
print(await get())
in a separate method and have it in yourinitState
.Like this:
您正在打印
await Storage.getValue(StorageKeys.authTokenKey);
值,并且由于它是 Future,因此您会收到此消息。您应该尝试在 initState 上调用它,然后获取 Hive 值。当该值返回时,您无法打印它。
例如:
You are printing the
await Storage.getValue(StorageKeys.authTokenKey);
value, and as it is a Future, you get this message.You should try to call it on your initState and then get the Hive value. When the value returns you cant print it.
Eg: