简体   繁体   English

Flutter 注销后应用登录 PostgresSQLException

[英]Flutter app login after a logout PostgresSQLException

I'm working on a Flutter app using Riverpod for the state management and Postgres for the database.我正在使用 Riverpod 进行 state 管理和 Postgres 数据库的 Flutter 应用程序。 I try to do a simple thing: display a message in the home screen with the nickname of the current user.我尝试做一件简单的事情:在主屏幕上显示一条带有当前用户昵称的消息。 When I logout and login to check if my feature works, my user is null and my console display a PostgresSQLException:当我注销并登录以检查我的功能是否有效时,我的用户是 null 并且我的控制台显示 PostgresSQLException:

PostgreSQLSeverity.error : Attempting to reopen a closed connection. Create a instance instead.

Any idea where I made a mistake?知道我在哪里犯了错误吗? My user_repository:我的用户存储库:

 PostgreSQLConnection connection = PostgreSQLConnection(
      '10.0.2.2', 5432, DatabaseAccess.databaseName,
      queryTimeoutInSeconds: 3600,
      timeoutInSeconds: 3600,
      username: DatabaseAccess.databaseUser,
      password: DatabaseAccess.databasePassword);

  Future<AppUser?> getCurrentUser() async {
    try {
      await connection.open();
      final result = await connection.mappedResultsQuery(
        'select * from public.user where email = @emailValue',
        substitutionValues: {
          'emailValue': email,
        },
        allowReuse: true,
        timeoutInSeconds: 30,
      );

      final userFromDataBase = result[0]['user']!;
      return AppUser(
        email: userFromDataBase['email'],
        nickname: userFromDataBase['nickname'],
        role: userFromDataBase['role'],
        firstname: userFromDataBase['firstname'],
        lastname: userFromDataBase['lastname'],
      );
    } on PostgreSQLException catch(e) {
      print(ErrorHandler(message: e.toString()));
      return null;
    }
  }

My screen:我的屏幕:

class HomeScreen extends HookConsumerWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final currentUser = ref.watch(currentUserProvider);
    return Scaffold(
      body: currentUser.when(
        data: (user) =>  _buildBody(context, user, ref),
        loading: () =>  const Center(child: CircularProgressIndicator()),
        error: (error, _) => _errorBody(context, ref),
      )
    );
  }

  Widget _buildBody(BuildContext context, AppUser? user, WidgetRef ref) {
    if(user == null) {
      return _errorBody(context, ref);
    } else {
      return Center(child: Text(
          'Welcome ${user.getNickname}',
        style: const TextStyle(fontSize: 20),
      ));
    }
  }

  Widget _errorBody(BuildContext context, WidgetRef ref) {
    return const Center(child: Text(
        "Error: No user found",
      style: TextStyle(fontSize: 20, color: Colors.red),
    ));
  }

}

I resolved this issue with the following code.我用下面的代码解决了这个问题。 It wasn't a postgres issue but a dark story about riverpod's providers.这不是 postgres 问题,而是有关 Riverpod 提供者的黑暗故事。

My providers.dart:我的供应商。dart:

final futureCurrentUserProvider = Provider<Future<AppUser?>>((ref) {
  return UserRepository().getCurrentUser(ref.watch(emailChangeProvider));
});

final currentUserProvider = FutureProvider.autoDispose<AppUser?>((ref) => UserRepository().getCurrentUser(ref.watch(emailChangeProvider)));

final authChangeProvider = StreamProvider<User?>((ref) {
  return ref.read(authRepositoryProvider).authUserChange;
});

final emailChangeProvider = Provider<String?>((ref) {
  return ref.watch(authChangeProvider).value?.email;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 angular 应用程序中注销后重定向到登录页面 - Redirect to login page after logout in angular app Flutter:如何注销 facebook 并获得登录对话框 - Flutter: how to logout of facebook and get the login dialog 注销后出错并尝试在 Firebase DB 上再次登录 - Error after logout and try to login again on Firebase DB React Native Firebase 注销 function 在根据用户类型登录后不起作用 - React Native Firebase logout function doesn't work after login based on user type Flutter:注册成功后如何让用户自动登录? - Flutter: How to make user login automatically after register successfully? 如何在Flutter中谷歌登录后重定向到另一个页面 - How to redirect to another page after google login in Flutter Firebase OTP 在将 flutter 应用程序上传到 Playstore 后无法正常工作 - Firebase OTP not working after uploading flutter app to Playstore 添加 cloud_firebase 包后,我的 Flutter 应用程序未运行 - My Flutter App is not running after adding cloud_firebase package Flutter 应用程序在调用 FacebookAuth.instance.login() 时无异常崩溃 - Flutter app crashing without exception when calling FacebookAuth.instance.login() 注销后出现缺少或权限不足的错误 - Missing or insufficient permissions errors after logout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM