Flutter async memorizer

834

I found a solution about it.

AsyncMemoizer _memorizer = AsyncMemoizer<List<bool>>();

Generic AsyncMemorizer works.

Share:
834
Cem Altuner
Author by

Cem Altuner

I'm studying at the Ege University Computer Engineering. I'm 21 and trying to learn new techs.

Updated on December 20, 2022

Comments

  • Cem Altuner
    Cem Altuner over 1 year

    I am using my own API. I only want to fetch data once when I log into my account. I found AsyncMemorizer.

    AsyncMemoizer _memorizer = AsyncMemoizer();
    

    I have 3 future functions and they return Future. Normally without AsyncMemorizer it works fine but in this case, I had an error.

    fetchData(Store store) {
        return this._memorizer.runOnce(() async {
          return await Future.wait([
            fCustomer(store),
            fList(store),
            fCompanies(store)
          ]);
        });
      }
    @override
      Widget build(BuildContext context) {
        Store store = StoreProvider.of<AppState>(context);
          return FutureBuilder<List<bool>>(
            future: this._fetchData(store),
            builder: (
                context,
                AsyncSnapshot<List<bool>> snapshot,
                ) {
    
              if (!snapshot.hasData) {
                return CircularProgressIndicator();
              }
    
    
              if (snapshot.data.every((result) => result == true)) {
                return screen();
              }
              return Text("Sıqıntı");
            },
          );
    
    
        }}
    

    error:

    The following assertion was thrown building HomeScreen(dirty, dependencies: [StoreProvider<AppState>], state: _HomeScreenState#79c31(tickers: tracking 1 ticker)):
    type 'Future<dynamic>' is not a subtype of type 'Future<List<bool>>'