Flutter Riverpod Async Notifier | Unhandled Exception: Bad state: Future already completed

1k Views Asked by At

I am using the riverpod_generator, and the AsyncNotifier in riverpod

part 'auth_controller.g.dart';

// 3. annotate
@riverpod
// 4. extend like this
class AuthController extends _$AuthController {
  // 5. override the [build] method to return a [FutureOr]
  @override
  FutureOr<void> build() {
    // 6. return a value (or do nothing if the return type is void)
  }

  Future<void> signIn({
    required String email,
    required String password,
  }) async {
    final authRepository = ref.read(authRepositoryProvider);
    state = const AsyncLoading();
    state = await AsyncValue.guard(
        () async => await authRepository.signIn(email, password));
  }

  Future<void> logout() async {
    state = const AsyncLoading();
    state = await AsyncValue.guard(() async {
      await FirebaseAuth.instance.signOut();
    });
  }
}

So when I am trying to logout the error will be occur it said

Future already completed

How to solve this?

1

There are 1 best solutions below

0
On

I solve this problem, actually I listen to the changes of FirebaseAuth currentUser, then it will automatically redirect either homepage (if logged in) or login page (if logout), therefore the error will be occurred because I subscribe to the changes of FirebaseAuth