Hive for flutter 仅返回实例而不是实际值

发布于 2025-01-11 15:48:51 字数 1783 浏览 0 评论 0原文

我决定使用 hive 作为我的设置/首选项存储。但是,我无法正确实现我的 Storage 类,因为 getValue 方法始终返回 'Future' 实例,而不是实际值。有谁知道如何解决这个问题?

我的 Storage 类仅包含 getValuesetValue ,它们始终打开配置单元框,然后应该设置或获取值。此外,我还创建了枚举 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2025-01-18 15:48:51

print(get()); 将为您提供 Future 实例,因为 get() 返回一个 Future 对象。

解决方案:

您需要在 Future 方法中的 get() 之前编写 await 来等待 Future 对象中的实际值。

像这样:

print(await get());

在上面的问题中,这不能工作,因为构建方法不能是异步的。您可以将 print(await get()) 放在单独的方法中,并将其放在 initState 中。

像这样:

 @override
  void initState() {
    super.initState();
    callGet();
  }

  Future<void> callGet() async {
    print(await get());
  }

print(get()); will give you Instance of Future<dynamic> since get() returns a Future object.

SOLUTION:

You need to await the actual value in the Future object by writing await before get() in a Future method.

Like this:

print(await get());

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 your initState.

Like this:

 @override
  void initState() {
    super.initState();
    callGet();
  }

  Future<void> callGet() async {
    print(await get());
  }
宁愿没拥抱 2025-01-18 15:48:51

您正在打印 await Storage.getValue(StorageKeys.authTokenKey); 值,并且由于它是 Future,因此您会收到此消息。

您应该尝试在 initState 上调用它,然后获取 Hive 值。当该值返回时,您无法打印它。

例如:

  @override
  void initState() {
    super.initState();
    Storage.getValue(StorageKeys.authTokenKey).then((value) => print(value));
  }

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:

  @override
  void initState() {
    super.initState();
    Storage.getValue(StorageKeys.authTokenKey).then((value) => print(value));
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文