0

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?

2
  • ensure that response's data and error get initialized, it's a deserialization problem Commented Apr 4, 2024 at 19:09
  • I'm not quite sure where to do that, how would I go about initializing them? Commented Apr 4, 2024 at 22:25

1 Answer 1

0

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']);
}

 


 
     
      
     
     
    
  
     
    
  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.