fluttersupabase-database

having trouble with login functionality with flutter supabase


I have created login screen which fetch data from supabase database and user can login when that login details got true and user can login.

onPressed: () async {
                    final supabase = Supabase.instance.client;
                    final email = emailController.text;
                    final password = passwordController.text;

                    // Authenticate user with Supabase
                    final response = await supabase.auth.signInWithPassword(
                      email: email,
                      password: password,
                    );

                    // ignore: unnecessary_null_comparison
                    if (response.error == null) {
                      // Show success popup
                      showDialog(
                        // ignore: use_build_context_synchronously
                        context: context,
                        builder: (BuildContext context) {
                          return AlertDialog(
                            title: const Text('Success'),
                            content: const Text('Login Successful'),
                            actions: <Widget>[
                              TextButton(
                                onPressed: () {
                                  Navigator.of(context).pop(); // Close dialog
                                  Navigator.pushReplacement(
                                    context,
                                    MaterialPageRoute(
                                      builder: (context) => const Home(),
                                    ),
                                  );
                                },
                                child: const Text('OK'),
                              ),
                            ],
                          );
                        },
                      );
                    } else {
                      // Login failed, handle the error
                      // ignore: avoid_print
                      print('Login failed: ${response.error!.message}');
                      // Show an error message to the user
                    }
                  },

There is only one problem in If/Else where if(response.error==null) gives this error The getter 'error' isn't defined for the type 'AuthResponse'. Try importing the library that defines 'error', correcting the name to the name of an existing getter, or defining a getter or field named 'error'.


Solution

  • Do not do it like that, rather wrap your code in a try-catch block like this

      try {
        final supabase = Supabase.instance.client;
        final email = emailController.text;
        final password = passwordController.text;
        // Authenticate user with Supabase
        await supabase.auth.signInWithPassword(
          email: email,
          password: password,
        );
        showDialog(
          // ignore: use_build_context_synchronously
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: const Text('Success'),
              content: const Text('Login Successful'),
              actions: <Widget>[
                TextButton(
                  onPressed: () {
                    Navigator.of(context).pop(); // Close dialog
                    Navigator.pushReplacement(
                      context,
                      MaterialPageRoute(
                        builder: (context) => const Home(),
                      ),
                    );
                  },
                  child: const Text('OK'),
                ),
              ],
            );
          },
        );
      } on AuthException catch (e) {
        print('Login failed: ${e.message}');
      }
    

    refer to this https://pub.dev/documentation/supabase/latest/supabase/AuthException-class.html