Skip to content
Back

[SOLVED] Unable to create new user in dart async function

  • 0
  • Flutter
  • Accounts
Onyx4499
15 Apr, 2023, 05:41

Whenever I try to await the creation of a new user using account.create() function of the flutter SDK , my async function just gets stuck at the await statement and does not proceed past it. When I check the appwrite console to see if the user was created or not I find that a new user was also not created. But if I just convert my function into a simple synchronous function and call the account.create() method without waiting for the response, my function completes execution (which it should as I am not awaiting for the response) and a new user is also created which can be seen on the appwrite console. Any suggestions to why I am facing this?

TL;DR
The user was unable to create a new user using the `account.create()` function in Dart's async function. The issue was resolved by adding an `on AppwriteException catch (e) {}` block to catch the exception and log the error. The non-working code snippet was using `await` which caused the function to get stuck at the await statement and not proceed further. Converting the function into a synchronous function without waiting for the response allowed the function to complete execution and create a new user.
Onyx4499
15 Apr, 2023, 05:41

Working code snippet:

TypeScript
  void register({
    required String username,
    required String password,
  }) {
    try {
      _logger.log(Level.INFO, "Register method called of Authentication Repo.");
      _logger.log(Level.INFO, "$_client");
      _logger.log(Level.INFO, "$_account");

      final user = _account.create(
          userId: ID.unique(), email: username, password: password);

      _logger.log(Level.INFO, "Reached here");
      //_controller.add(AuthenticationStatus.authenticated);
    } on AppwriteException {
    } catch (e) {
      _logger.log(Level.SEVERE, "$e");
    } finally {}
  }

Execution Log:

TypeScript
I/flutter (26475): INFO: 2023-04-15 10:59:42.667754: Register method called of Authentication Repo.
I/flutter (26475): INFO: 2023-04-15 10:59:42.668095: Instance of 'ClientIO'
I/flutter (26475): INFO: 2023-04-15 10:59:42.668442: Instance of 'Account'
I/flutter (26475): INFO: 2023-04-15 10:59:42.703901: Reached here

Non-Working code snippet:

TypeScript
  Future<void> register({
    required String username,
    required String password,
  }) async {
    try {
      _logger.log(Level.INFO, "Register method called of Authentication Repo.");
      _logger.log(Level.INFO, "$_client");
      _logger.log(Level.INFO, "$_account");

      final user = await _account.create(
          userId: ID.unique(), email: username, password: password);

      _logger.log(Level.INFO, "Reached here");
      //_controller.add(AuthenticationStatus.authenticated);
    } on AppwriteException {
    } catch (e) {
      _logger.log(Level.SEVERE, "$e");
    } finally {}
  }

Execution Log:

TypeScript
I/flutter (26475): INFO: 2023-04-15 11:05:43.742769: Register method called of Authentication Repo.
I/flutter (26475): INFO: 2023-04-15 11:05:43.743017: Instance of 'ClientIO'
I/flutter (26475): INFO: 2023-04-15 11:05:43.743228: Instance of 'Account'
Drake
15 Apr, 2023, 14:37

It looks like you aren't logging if an AppwriteException occurs. Would you please try to use await and log that error

Onyx4499
16 Apr, 2023, 00:18

Sorry for the late reply. That was the reason. While I could not reproduce the exact situation again, I intentionally made an exceptional situation by making the password length to less than 8 characters and running the above snippet. Since I am not logging anything in the on block it seemed that it got stuck while it was actually throwing an error. I thought that the catch block would still execute if the on block condition matches for which I wrote the code like that but it is not the case. So I found out that the exact behaviour I was looking for was on AppwriteException catch (e) {} (As this would match the exception type as well as give me a copy of the exception object).

Onyx4499
16 Apr, 2023, 00:19

Thanks again!

Drake
16 Apr, 2023, 00:20

[SOLVED] Unable to create new user in dart async function

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more