rodydavis / signals.dart

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

UI Not Updating with Data in Watch.builder or signal.watch(context) #264

Closed iSaqibShafique closed 1 month ago

iSaqibShafique commented 1 month ago

Hey @rodydavis, hope you're well. Having an issue with Watch.builder or signal.watch(context) - UI isn't building when there's data. Take this example: I've got a FutureSignal starting with null. Upon tapping, it kicks off a function. But when the signal gets data, the UI doesn't update.

//! Business Logic

FutureSignal<String?> future = FutureSignal(() async => null);

void onTap() {
  future = FutureSignal(() async {
    return Future.delayed(const Duration(seconds: 5), () {
      return "Hello";
    });
  });
}

//! UI Code

Scaffold(
  body: Center(
    child: Watch.builder(
      builder: (context) {
        return controller.future.value.maybeMap(
          data: (value) => Text(value ?? "Null Now"),
          error: (e) => Text(e),
          loading: () => const CircularProgressIndicator(),
          orElse: () => const Text("Not Loaded"),
        );
      },
    ),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: () => controller.onTap(),
  ),
);
iSaqibShafique commented 1 month ago

It starts working when I do "Hot Reload"

rodydavis commented 1 month ago

You need to set rate on the onTap because you are initializing late

iSaqibShafique commented 1 month ago

Sure thing! 🌟 @rodydavis, mind expanding a bit with an example? Much appreciated! 🙏