FlutterRiverpod异步通知器|未处理的异常:错误状态:将来已完成

yh2wf1be  于 2023-01-31  发布在  Flutter
关注(0)|答案(1)|浏览(128)

我使用的是riverpod_generator和riverpod中的AsyncNotifier

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();
    });
  }
}

因此,当我试图注销时,错误将发生,它说
未来已完成
如何解决这个问题?

vfh0ocws

vfh0ocws1#

我解决了这个问题,实际上我听了FirebaseAuth currentUser的变化,然后它会自动重定向主页(如果登录)或登录页面(如果注销),所以会出现错误,因为我订阅了FirebaseAuth的变化

相关问题