skip to Main Content

so i have created this method that does 2 things, one after another, the first thing this method does is sign up the user using supabase, the second thing is it saves the users info on a table, here is a how the method looks :

Future signUp(
  String email, String password, String firstName, String lastName) async {
try {
  // Step 1: Register the user using Supabase authentication
  final response =
      await supabase.auth.signUp(password: password, email: email);
  if (response.user == null) {
    print('Sign up NOT successful!');
    throw "failed to sign up";
  } else {
    print('Sign up successful!');
  }

  // Step 2: Retrieve the user ID of the newly created user
  final userId = response.user!.id;

  // Step 3: Insert user's first name and last name into the user_info table

  final insertResponse = await supabase.from('user_info').insert(
      {'id': userId, 'first_name': firstName, 'last_name': lastName});

  if (insertResponse.error != null) {
    print('user info not registered');
    throw insertResponse.error!;
  }

  // If everything is successful, the sign-up process is complete
  print('Sign up and user table successful!');
} catch (error) {
  print('Error during sign-up: $error');
}

}
what’s weird is when i check the user table and the ‘user_info’ table on the website i can see that everything works, yet when i check the log console i get this :
I/flutter ( 2765): Sign up successful!
I/flutter ( 2765): Error during sign-up: NoSuchMethodError: The getter ‘error’ was called on null.
I/flutter ( 2765): Receiver: null
I/flutter ( 2765): Tried calling: error
What i don’t get is why am getting an error on the log, but it still works when i check the database

2

Answers


  1. this means that insertResponse might be null, so you should update your error checking code to be like insertResponse?.error != null and dont throw it liket this
    throw insertResponse.error!;

    throw the entire thing

      throw insertResponse?.error; 
    
    Login or Signup to reply.
  2. I advise you to type your variables (you can also use a analysis_options.yaml file with linter rules to help you keep clean code).

    And yes @jhon is right, your error message means insertResponse might be null so you cannot call insertResponse.error directly.

    In your if, you can do :

    • if insertResponse == null means an error occured:
    if (insertResponse == null || insertResponse.error != null)
    
    • if insertResponse == null means everything is fine :
    if (insertResponse != null && insertResponse.error != null)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search