I've run into a problem where the app says 'User not found. Please check credentials.' although the credentials used is for user in the profiles user_profiles table. Is this because response of the query to the database is a list, or something else? I've also tried adding the .single() command and got the error
"Login failed: PostgrestException(message: JSON object requested, multiple (or no) rows returned, code: PGRST116, details: The result contains 0 rows, hint: null)"
I've attached the image of the user_profiles table in the database.
supabase database user_profiles table:
Main file
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
class CustomLoginPage extends StatefulWidget {
const CustomLoginPage({Key? key}) : super(key: key);
@override
_CustomLoginPageState createState() => _CustomLoginPageState();
}
class _CustomLoginPageState extends State<CustomLoginPage> {
final TextEditingController _firstNameController = TextEditingController();
final TextEditingController _lastNameController = TextEditingController();
final TextEditingController _idNumberController = TextEditingController();
bool _isLoading = false;
Future<void> _signIn() async {
setState(() {
_isLoading = true;
});
final client = Supabase.instance.client;
try {
final response = await client
.from('user_profile')
.select()
.eq('first_name', _firstNameController.text.trim())
.eq('last_name', _lastNameController.text.trim())
.eq('id_number', _idNumberController.text.trim())
//.single();
if (response.isNotEmpty) {
// Handle successful authentication.
if (context.mounted) {
Navigator.of(context).pushReplacementNamed('/account');
}
} else {
// Handle user not found.
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('User not found. Please check your credentials.'),
backgroundColor: Colors.red,
),
);
}
}
} catch (err) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Login failed: ${err.toString()}'),
backgroundColor: Theme.of(context).colorScheme.error,
),
);
}
}
if (context.mounted) {
setState(() {
_isLoading = false;
});
}
}
void _showSnackBar(String message) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
backgroundColor: Theme.of(context).colorScheme.error,
),
);
}
}
@override
void dispose() {
_firstNameController.dispose();
_lastNameController.dispose();
_idNumberController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Custom Sign In')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
TextFormField(
controller: _firstNameController,
decoration: const InputDecoration(labelText: 'First Name'),
),
TextFormField(
controller: _lastNameController,
decoration: const InputDecoration(labelText: 'Last Name'),
),
TextFormField(
controller: _idNumberController,
decoration: const InputDecoration(labelText: 'ID Number'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _isLoading ? null : _signIn,
child: Text(_isLoading ? 'Loading...' : 'Sign In'),
),
],
),
);
}
}
The app should be able to read the user_profiles table and if there is a row that matches the login credentials, then sign in should be successful and the user should be directed to the account page.
