Back

Function to create a new account. (Dart - Flutter - cloud - function)

  • 1
  • Flutter
  • Functions
  • Cloud
josePergo
9 Apr, 2024, 19:33

I am using a function to create a new user account (those that appear in the Auth section of the console) and I am doing it in dart. I created the function by selecting dart 3.1 and when it was created it did so using dart 2.17 with dart_appwrite version 8.0.0 and it did not allow me to create an account with those versions, so I had to update the dart_appwrite 11.0.2 parameters and environment: sdk: ^3.1.0 to enable me to create a new account. This is the code I am using for the function

TL;DR
Function to create a new account in Dart for a Flutter cloud function. An error "type 'Null' is not a subtype of type 'bool" is occurring. Code updates required due to SDK version issue from 8.0.0 to 11.0.2. Adjustments needed in the code for the Account creation process to be successful.
josePergo
9 Apr, 2024, 19:33

import 'dart:async'; import 'dart:convert'; import 'package:dart_appwrite/dart_appwrite.dart';

Future<dynamic> main(final context) async { String message = 'error'; if (context.req.body != null && context.req.body != '' && context.req.method == 'POST') { context .log('entro aqui ya que viene por POST con esto ${context.req.body}'); try { final client = Client() .setEndpoint('https://cloud.appwrite.io/v1') .setProject('MIPROYECTO'); final account = Account(client);

TypeScript
  final formData = jsonDecode(context.req.body) as Map<String, dynamic>;
  final email = formData['email'].toString();
  final password = formData['password'].toString();
  final username = formData['username'].toString();
  context.log(
      'estos datos llegan a la funcion $email - $password - $username');
  await account
      .create(
    userId: ID.unique(),
    email: email,
    password: password,
    name: username,
  )
      .then((value) async {
    context.log('se crea la cuenta');
    context.log('value = $value');
    message =
        'Se creo la cuenta de usuario con email: $email y password: $password';
  }).catchError((onError) {
    // context.log('onError al crear la cuenta account =====> $onError');
    message = 'onError al crear la cuenta account =====> $onError';
  });
} on AppwriteException catch (e) {
  // context.log('ocurrio un error= $e');
  message = e.message ?? 'error - e = $e';
}

} context.log('message: $message'); return context.res.json({ 'message': message, }); }

I call the function from my app developed in Flutter to run and the new account is created because I can see it in the console in the Auth section but when I go to the Function Executions section in the response I see the following error

josePergo
9 Apr, 2024, 19:33

message: onError creating account account =====> type 'Null' is not a subtype of type 'bool'

I would like to know the reason for the error

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