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?
Working code snippet:
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:
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:
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:
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'
It looks like you aren't logging if an AppwriteException occurs. Would you please try to use await and log that error
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).
Thanks again!
[SOLVED] Unable to create new user in dart async function
Recommended threads
- android platform invaild origina
It happened today suddenly. Our app says invalid origin. And appwrite cloud says every time we tried to add the app to it: "param platformId" is not optional.
- Team invite - 500 error - no email
When executing ```dart await _repository.teams.createMembership( teamId: event.listId, roles: ['member'], email: event.email, url: 'xxxx', ); ``` I se...
- Help with nameservers
I just added our domain, and as per instruction in the page following, it says, "Add the following nameservers on your DNS provider. ..." I want to keep my cu...