skip to Main Content

I am new to flutter and firebase, so please forgive me if this is a silly question.

I am trying to read a document from Cloud Firestore and place the data in a defined object called Profile.

This is my current implementation:

final docRef = FirebaseFirestore.instance
                .collection('/user_data')           
                .doc(FirebaseAuth.instance.currentUser!.uid);

Future<Profile> readUserDocument() {    
    return docRef.get().then(
        (DocumentSnapshot doc) {
            return Profile(
                imagePaths: doc.get('imagePaths'), 
                firstName: doc.get('firstName'),
                lastName: doc.get('lastName') , 
                bio: doc.get('bio'), 
                gender: doc.get('gender'), 
                hometown: doc.get('hometown'), 
                age: doc.get('age'), 
                ethnicity: doc.get('ethnicity'), 
                hairColor: doc.get('hairColor'), 
                eyeColor: doc.get('eyeColor'), 
                hobbies: doc.get('hobbies'), 
                instagram: doc.get('instagram'), 
                snapchat: doc.get('snapchat'), 
                work: doc.get('work'), 
                school: doc.get('school'), 
                educationLevel: doc.get('educationLevel')
            );
        },
        onError: (e) => print("Error getting document: $e"),
    );
}

In a separate file, I define a list tile that calls readUserDocument() in the onClick method

ListTile(
    onTap: () async {
        Profile userData = await readUserDocument(); // TYPE ERROR HERE
        // ...
    }
    // ...
)
              

When calling await readUserDocument(), the following exception is thrown:

_TypeError (type 'List<dynamic>' is not a subtype of type 'List<String>')

This confuses me because readUserDocument() will return a Profile, not a List<dynamic>. I am also confused as to why List<String> is involved here.

Any help would be appreciated. Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    I ended up taking a different approach and following the Firestore documentation example.

    To my Profile object, I added the factory Profile.fromFirestore() and Map<String, dynamic> toFirestore() methods. I was then able to use a converter to get the Profile object

    Future <Profile> getUserProfile() async {   
        final ref = FirebaseFirestore.instance
            .collection("/user_data")
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .withConverter(
        fromFirestore: Profile.fromFirestore,
        toFirestore: (Profile profile, _) => profile.toFirestore(),
        );
        
        final docSnap = await ref.get();
        Profile profile = docSnap.data() ?? constants.NULL_PROFILE;
        return profile;
    }
    

  2. Maybe you can try get the document snapshot and build the object when returned to the ListTile

    final docRef = FirebaseFirestore.instance
                    .collection('/user_data')           
                    .doc(FirebaseAuth.instance.currentUser!.uid).snapshots();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search