skip to Main Content

Hey guys so I need to get a field value from the firestore in order to use it in my code for some reason I created this method to get the value for me but it always returns Instance of ‘Future’ even though the field I’m returning is a String
anyone know what is the issue here

getTheNextUserId() async{
var collection = FirebaseFirestore.instance.collection('collId');
var docSnapshot = await collection.doc('dId').get();
if (docSnapshot.exists) {
  Map<String, dynamic> data = docSnapshot.data()!;
  var uId = data['nextUserId'];
  return uId;
}
Future addUserDetails(String firstName, String lastName, int age,String email) async {
  final CollectionReference userRef = FirebaseFirestore.instance.collection('user');
  await userRef.doc(getTheNextUserId.toString()).set({
  'first name': firstName,
  'last name': lastName,
  'age': age,
  'email': email,
  'count': 10,});
}

2

Answers


  1. Update your getTheNextUserId to this:

    Future<String> getTheNextUserId {
    ...
    }
    

    then do this in addUserDetails:

    var str = await getTheNextUserId();
    
    await userRef.doc(str).set({
      'first name': firstName,
      'last name': lastName,
      'age': age,
      'email': email,
      'count': 10,});
    
    Login or Signup to reply.
  2. Your getTheNextUserId is an async function, so it also must be awaited:

    Future addUserDetails(String firstName, String lastName, int age,String email) async {
      final CollectionReference userRef = FirebaseFirestore.instance.collection('user');
      await userRef.doc((await getTheNextUserId()).toString()).set({ // in this line
        'first name': firstName,
        'last name': lastName,
        'age': age,
        'email': email,
        'count': 10,
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search