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

1.3k Views Asked by At

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. 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. But i using getx and i want to take in stateless widget. Is it possible ?

1

There are 1 best solutions below

0
On

You can simply add another FutureBuilder inside the first one to await this second call.

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();
      },
    );
  },
)