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
- Appwrite (Dart SDK) (for package_info_pl...
Hi team! Many Flutter plugins now need package_info_plus 10.x and device_info_plus 13.x, but appwrite 25.4.0 still constrains package_info_plus: >=8.0.2 <10.0...
- Images/Videos in storage appearing broke...
I am running into a storage issue after upgrading my self-hosted Appwrite instance , all of my previously uploaded images and videos are now showing as broken i...
- executeFunction intermittently throws Fo...
Environment: Flutter app using the Appwrite Flutter SDK, calling executeFunction for [describe endpoint, e.g. live-stream-related function]. *Description*: Int...