Reputation: 3
I's working on an app that retrieves data from a flutter database but I'm having issues with my fetch queries.
Fetch method:
void _fetchUserData() async {
// Assuming you store the current user session and it has a valid token
final user = Supabase.instance.client.auth.currentUser;
if (user != null) {
setState(() {
_userId = user.id; // Set the user ID
});
// Fetch the user profile data
final response = await Supabase.instance.client
.from('user_profiles')
.select()
.eq('id', user.id)
.single();
if (response.data != null) {
// Update the state with fetched data
setState(() {
// Assuming 'date_of_birth', 'gender', and 'id_number' are columns in your 'profiles' table
_dobController.text = response.data['date_of_birth'];
_genderController.text = response.data['gender'];
_idController.text = response.data['id_number'];
});
} else if (response.error != null) {
// Handle the error
_showSnackBar(response.error.message);
}
}
}
Error messages: The getter 'data' isn't defined for the type 'Map<String, dynamic>'. Try importing the library that defines 'data', correcting the name to the name of an existing getter, or defining a getter or field named 'data'.
The getter 'error' isn't defined for the type 'Map<String, dynamic>'. 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'.
I'm not sure why I'm getting this error message as I've seen many examples using response.data and response.error. Is there a fix for this or an alternative method of getting the response?
Upvotes: 0
Views: 124
Reputation: 34
First, you need to parse your response into JSON format, then you can use your response. Please find the below code; I think this code will help you.
Map<String,dynamic> responseData=json.decode(response) as Map<String, dynamic>;
if (responseData['data'] != null) {
setState(() {
_dobController.text = responseData['data']['date_of_birth'];
_genderController.text =responseData['data']['gender'];
_idController.text = responseData['data']['id_number']; });
} else if (responseData['error'] != null) {
_showSnackBar(responseData['error']['message']);
}
Upvotes: 0