简体   繁体   English

在 Flutter 中,有没有办法在小部件的颤振安全存储中获取项目?

[英]In Flutter, is there a way get item in flutter secure storage in widget?

I want to take a data from flutter secure storage.我想从颤振安全存储中获取数据。 But as you know its a async function for get it.但是你知道它是一个异步函数来获取它。 So in my widget , how can i take that data ?所以在我的小部件中,我怎样才能获取这些数据? I cant use await for take it.我不能使用 await 来接受它。 So how can i take it ?那么我该如何接受呢? Thanks for response!感谢您的回复!

Code :代码 :

 FutureBuilder<PurchasedPackageResponseModel?>(
            future: service.getPurchasedPackages(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return const Center(
                  child: CircularProgressIndicator(),
                );
              }
 I want to take data here ==>    bool isClientTherapist = (I cant use await here So its giving error.) UserSecureStorage.getField("isClientTherapist");

          
              final model = snapshot.data!.data!;
              if (model.isEmpty) {
                return const NotBoughtSessionWidget();
              }

              return const SizedBox();

If i make it stateful widget i can take in initstate.如果我让它成为有状态的小部件,我可以接受 initstate。 But i using getx and i want to take in stateless widget.但是我使用 getx 并且我想采用无状态小部件。 Is it possible ?可能吗 ?

You can simply add another FutureBuilder inside the first one to await this second call.您可以简单地在第一个中添加另一个 FutureBuilder 来等待第二次调用。

FutureBuilder<PurchasedPackageResponseModel?>(
  future: service.getPurchasedPackages(),
  builder: (context, snapshot1) {
    if (!snapshot1.hasData) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    }

    return FutureBuilder<bool?>(
      future: UserSecureStorage.getField("isClientTherapist"),
      builder: (context, snapshot2) {
        if (!snapshot2.hasData) {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }

        final isClientTherapist = snapshot2.data!;
        final model = snapshot1.data!.data!;
        if (model.isEmpty) {
          return const NotBoughtSessionWidget();
        }
        return const SizedBox();
      },
    );
  },
)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM