Reputation: 85
Related to my previous posting here about Flutter and Supabase, how can I handle the results of my queries using Supabase? It is not clear in the Supabase documentation about handling the returned results including error checking. I need a code snippet of the querying and handling the result/storing it in a variable/class. In my code, I am trying to check the credentials of a respondent logging in. I have tried to use Microsoft Bing/Copilot/ChatGPT, but it showed deprecated solutions, like queries with a .execute() and some response.data. My incomplete code snippet:
ElevatedButton(
onPressed: () async {
final supabase = Supabase.instance.client;
final response = await supabase
.from('respondents')
.select()
.eq('household_number', _householdNumberController.text)
.eq('pin_password', _pinPasswordController.text)
.maybeSingle();
// Need to overhaul this part
print(response);
if (response != null) {
print('Ok');
} else {
print('Not ok');
}
//
},
child: const Text('Login'),
),
If the result is just one entry, I need to save the details and pass/redirect to a new page, let's say SurveyForm. I also need to handle if the result is null or have multiple entries, since both will generate an error message.
The answers I have seen have used the Supabase auth. As I have mentioned in my previous posting, I need a different authentication method, which is just a simple check if a query will return something. Unfortunately, I have not seen a sample code yet or have not been using the right words to query it in the web.
[Update] This is the code snippet Microsoft Bing/ChatGPT answered but it does not work since .execute() is deprecated and response.data is not existing anymore. I need an updated version of this code.
import 'package:flutter/material.dart';
import 'package:supabase/supabase.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _householdNumberController = TextEditingController();
final _pinController = TextEditingController();
Future<void> _login() async {
final householdNumber = _householdNumberController.text;
final pin = _pinController.text;
final client = SupabaseClient('supabaseUrl', 'supabaseKey');
final response = await client
.from('Respondents')
.select()
.eq('household_number', householdNumber)
.eq('pin', pin)
.execute();
if (response.data != null && response.data.isNotEmpty) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SurveyForm()),
);
} else {
// Handle login failure...
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: <Widget>[
TextField(
controller: _householdNumberController,
decoration: InputDecoration(labelText: 'Household Number'),
),
TextField(
controller: _pinController,
decoration: InputDecoration(labelText: 'PIN'),
),
ElevatedButton(
onPressed: _login,
child: Text('Login'),
),
],
),
),
);
}
}
class SurveyForm extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Build your survey form here...
return Scaffold(
body: Center(
child: Text('Survey Form'),
),
);
}
}
Upvotes: 1
Views: 1269
Reputation: 91
You can get the count for this data using the following way -
To get the count of users
this.supabase.from('respondents').select('count', { count: 'exact' });
Then you can get a number of respondents fulfilling your requirements.
Let me give you a simple example: -
var users = await supabase.from('user_table').select(); //You will get an array here.
var isAuthenticated = users.length > 0 && users.length <=1; // Here true will conclude that the user is single and authenticated.
//And to find multiple users info is the following question.
for (var i = 0; i < users.length; i++) {
print("Name : " + users[i]['name']);
print("Last name : " + users[i]['lastName']);
}
Upvotes: 3