seyan
seyan

Reputation: 31

Flutter Supabase Auth signUp Error: _asyncStorage != null: You need to provide asyncStorage to perform pkce flow

I'm working on a Flutter app and using Supabase for authentication. When I try to sign up a user using email address, I encounter the following error:

'package:gotrue/src/gotrue_client.dart': Failed assertion: line 201 pos 16: '_asyncStorage != null': You need to provide asyncStorage to perform pkce flow.

This my code: supabase_client.dart

import 'package:supabase_flutter/supabase_flutter.dart';

class SupabaseClientService {
final SupabaseClient client;

SupabaseClientService._internal()
client = SupabaseClient(
           'http',
           'annon-key',
         );

static final SupabaseClientService instance = SupabaseClientService._internal();
}

This is the part of my registration.dart where the error is occuring.

part of registration.dart

Future<void> _registerUser() async {
  if (_formKey.currentState!.validate()) {
    try {
      final response = await supabase.auth.signUp(
        email: _emailController.text.trim(),
        password: _passwordController.text.trim(),
      );

      if (response.user != null) {
        await supabase.from('users').insert({
          'id': response.user!.id,
          'email': _emailController.text,
          'username': _usernameController.text,
        });

        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Registration successful. Please verify your email.')),
        );
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => const LoginPage()),
        );
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Registration failed. Please try again.')),
        );
      }
    } on PostgrestException catch (error) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error.message)));
    } catch (error) {
      print(error);
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('An unexpected error occurred.')),
      );
    }
  }
}

Specifically this line: final response = await supabase.auth.signUp.

How can I resolve the error 'package:gotrue/src/gotrue_client.dart': Failed assertion: line 201 pos 16: '_asyncStorage != null': You need to provide asyncStorage to perform pkce flow. when using Supabase Auth in my Flutter app? What am I missing or doing incorrectly in my setup?

  1. I've ensured that Supabase is properly initialized in my supabase_client.dart.
  2. All dependencies in pubspec.yaml are up-to-date.
  3. Verified that environment variables for Supabase URL and anon key are correctly set.
  4. Watched yt tutorials and they didn't have any errors that I had while using the same authentication logic.

Upvotes: 1

Views: 218

Answers (1)

Ahmad M.
Ahmad M.

Reputation: 524

Changed the options and it worked:

   _supabaseClient = SupabaseClient(
      supabaseUrl!,
      supabaseAnonKey!,
      authOptions: const AuthClientOptions(authFlowType: AuthFlowType.implicit),
    );
  }

Upvotes: -1

Related Questions