Search code examples
dartstoredart-async

retrieve multiple stores in dart


I use boilerplate code to retrieve data from 1 store such as

MonthStore monthStore = new MonthStore();
monthStore.open().then((months) {

but I am having difficulty retrieving data from multiple, related stores. Is this the best I can do?

MonthStore monthStore = new MonthStore();
monthStore.open().then((months) {
  TranStore tranStore = new TranStore();
  tranStore.open().then((trans) {

    // months[trans.monthId].name

  });
});

I tried using Future.wait like this

// declare the stores
MonthStore monthStore = new MonthStore();
TranStore tranStore = new TranStore();

Future.wait(
  [
    getMonth(monthStore, intMonth),
    // another call
  ]
)
.then...

Future<Map> getMonth(mnthStore, mnth) {
  mnthStore.open()
    .then((mnths) {
      return mnths[mnth];
    })
  // need a return here!
});

but the editor says no return specified in the Future.

What am I missing here?


Solution

  • Future<Map> getMonth(mnthStore, mnth) {
      return mnthStore.open() // <= here the return is important
      .then((mnths) {
        return mnths[mnth];
      });
    });