Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to wait ObservableFuture.status in function? #984

Open
Poloten opened this issue Feb 1, 2024 · 0 comments
Open

How to wait ObservableFuture.status in function? #984

Poloten opened this issue Feb 1, 2024 · 0 comments
Labels
question Further information is requested

Comments

@Poloten
Copy link

Poloten commented Feb 1, 2024

I need to open Menu after get data. But after I wait getData method, the List is empty, but if I click second time, the data is exist.
Help me please.

  1. What's the better pattern ? Maybe I do this wrong.
  2. Why I need to wrap parent and child widgets in Observed ? (If I wrap only parent, status in child updated only once).

Store:

 @observable
  ObservableMap<String, ObservableFuture<List<UiItem>>> send = ObservableMap.of({});

  @action
  setDefault(String currency) {
    if (send[currency] == null) send[currency] = ObservableFuture.value([]);
  }

  @action
  Future<void> getSendMenu(String currency) async {
    if (send[currency] == null) setDefault(currency);

    send[currency] = ObservableFuture(getSendMenuItems(currency));

    await send[currency];
  }

child widget

 ObservableFuture<List<UiItem>>? menuObject;

  @override
  Widget build(BuildContext context) {
    return Observer(builder: (_) {
      return OutlinedButton(
        onPressed: () => getMenu().then((_) async => {
             // menuObject?.result  - is exist after second click;
              showMenu(context, menuObject?.result)
            }),
        loading: menuObject != null && menuObject?.status == FutureStatus.pending,
        icon: icon ?? icon,
        text: '$text',
      );
    });
  }

parent widget

@override
Widget build(BuildContext context) {
  final store = Provider.of<MenuStore>(context);

 return Expanded(
    flex: 1,
    child: Observer(builder: (_) {
      return PopupMenuFuture(
        getMenu: () => store.getSendMenu(currency),
        icon: Icons.south_east,
        menuObject: store.send[currency],
        text: 'SEND',
      );
    }),
  ),
}

flutter_mobx: ^2.2.0+2

@Poloten Poloten changed the title ObservableFuture.result updated with latency but ObservableFuture.status work fine How to wait ObservableFuture.status in function? Feb 2, 2024
@amondnet amondnet added the question Further information is requested label Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants