rodydavis / signals.dart

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

Question about use of Future Signal. #267

Closed iSaqibShafique closed 1 month ago

iSaqibShafique commented 1 month ago

If we need to change the value of FutureSignal then how we can do this,I have searched all across the web, but haven't find any solution, like this.

FutureSignal<SignupStatus> loginSignal =
      FutureSignal(() async => SignupStatus.initial, autoDispose: true);

  void login(BuildContext context,
      {required String email, required String password}) {
    if (key.currentState!.validate()) {
      loginSignal = FutureSignal(() async {
        FirebaseAuth auth = FirebaseAuth.instance;
        UserCredential cred = await auth.createUserWithEmailAndPassword(
            email: email, password: password);
        if (cred.user != null) {
          await cred.user!.sendEmailVerification();
          // ignore: use_build_context_synchronously
          GoRouter.of(context).pushReplacementNamed(AppRoutes.emailverification,
              extra: {"user": cred.user});
          return SignupStatus.success;
        } else {
          return SignupStatus.error;
        }
      });
    }
rodydavis commented 1 month ago

Why not put that logic inside the future signal? This is create a new signal every time that will break subscribers.

If you want to do it manually there is also AsyncSignal

iSaqibShafique commented 1 month ago

@rodydavis

Why I'm getting this error :

flutter: signal warning: [4|null] has been read after disposed: #0 Signal.value (package:signals_core/src/core/signal.dart:292:97)

1 watchSignal (package:signals_flutter/src/watch/extension.dart:10:46)

2 FlutterReadonlySignalUtils.watch (package:signals_flutter/src/extensions/signal.dart:15:12)

3 _EmailSignupState.intialState (package:dating/view/email_signup/view.dart:108:50)

4 _EmailSignupState.build. (package:dating/view/email_signup/view.dart:71:22)

5 _WatchState.rebuild (package:signals_flutter/src/watch/widget.dart:196:34)

6 _WatchState.build. (package:signals_flutter/src/watch/widget.dart:219:11)

7 Effect._callback (package:signals_core/src/core/effect.dart:350:32)

8 _endBatch (package:signals_core/src/core/batch.dart:29:18)

9 Signal.set (package:signals_core/src/core/signal.dart:269:9)

10 Signal.value= (package:signals_core/src/core/signal.dart:284:5)

11 EmailSignupController.onRet<…>

When doing these changes : void onRetry() { loginSignal.value = const AsyncData(null); }

rodydavis commented 1 month ago

Because autoDispose is set to true, try removing that

iSaqibShafique commented 1 month ago

Same issue, I've tried removing autoDispose and set the autoDispose to false, but same issue occurs.

rodydavis commented 1 month ago

Can you provide a minimal code example to reproduce the issue?

rodydavis commented 1 month ago

Also try hot restart instead of hot reload to see if the issue still persists

rodydavis commented 1 month ago

If you are looking to pass args to update a future signal then you need to use computedAsync and computedFrom