rodydavis / signals.dart

Reactive programming made simple for Dart and Flutter
http://dartsignals.dev
Apache License 2.0
437 stars 50 forks source link

maybeMap doesn't map properly - loading state with orElse is not picked up #258

Closed daniel-v closed 4 months ago

daniel-v commented 5 months ago

Given the following scenario:

final saving = asyncSignal<bool?>(AsyncState.data(null));

// ...

Watch.builder(builder: (ctx) {
  return saving.value.maybeMap(
    loading: () => CircularProgressIndicator(),
    orElse: () => ElevatedButton(/*..*/)
  );
});

the circular progress indicator is shown all the time. I believe this is because of the implementation of AsyncState.maybeMap .

I think it should read:

    if (isLoading && loading != null) return loading();
    return orElse();

or rather in the style of existing code:

    if (isLoading) if (loading != null) return loading();
    return orElse();

map does not have this issue because all state callbacks have to be defined. (vale, error, loading)

rodydavis commented 5 months ago

Can you make a PR?

iSaqibShafique commented 5 months ago

@rodydavis, I encountered a similar issue while setting up a signal with a null value initially and then updating it onButtonClick. Even after assigning values to these signals (Future and Async), I faced the problem of UI not updating. However, I employed a switch functionality to handle this, as shown below:

Signal<List?> futureList = FutureSignal(() async => null);

// UI code return switch (futureList.watch(context)) { // Cases handling UI updates // // }

iSaqibShafique commented 5 months ago

Hey @rodydavis, any plans to iron out those Future and Async signal glitches? Considering using this state management in my next project. Thanks! 🙏

rodydavis commented 5 months ago

Yes for sure! Happy to accept a PR! Will get to it soon on the next batch of updates 👍🏼