Norbert
Norbert

Reputation: 7750

Dart Error: Unhandled exception: E/flutter ( 5079): Invalid argument: Instance of 'Future<String>'

I am trying to add document to my cloud firestore DB. In this manner.

Future<String> currentlyIn()async{
     FirebaseAuth auth = FirebaseAuth.instance;
    String fuser = await auth.currentUser();
    });
     return fuser.uid;
   }


Map<String, dynamic> votedown() {
    Map<String, dynamic> comdata = <String, dynamic>{
      'user_Id':currentlyIn(),
      'actual_vote':0,
      'voteUp': false,

    };
    return comdata;
  }

DocumentReference storeReference =Firestore.instance.collection('htOne').document('docq');
  await storeReference.setData(votedown());

However I get this error anytime I run the code. I need help on how to go about this successfully

E/flutter ( 6263): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception: 
E/flutter ( 6263): Invalid argument: Instance of 'Future<String>'
E/flutter ( 6263): #0      StandardMessageCodec.writeValue (package:flutter/src/services/message_codecs.dart:353:7) 
E/flutter ( 6263): #1      FirestoreMessageCodec.writeValue (file:///C:/NoFlutterPerms/Git/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.7.4/lib/src/firestore_message_codec.dart:38:13)

Upvotes: 6

Views: 26122

Answers (2)

vipin agrahari
vipin agrahari

Reputation: 2911

You are trying to use a Future<String> object as a String Hence it is giving the error. Try this code below.

  currentlyIn().then((value){
   Map<String, dynamic> comdata = <String, dynamic>{
  'user_Id': value,
  'actual_vote':0,
  'voteUp': false,
   };

  return comdata;
  });

Upvotes: 3

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 658205

currentlyIn returns a Future. You need to treat it as such. A Future doesn't automatically convert to the value it completes with.

You can use async/await like:

Future<Map<String, dynamic>> votedown() async {
    Map<String, dynamic> comdata = <String, dynamic>{
      'user_Id': await currentlyIn(),
      'actual_vote':0,
      'voteUp': false,

    };
    return comdata;
  }

DocumentReference storeReference =Firestore.instance.collection('htOne').document('docq');
  await storeReference.setData(await votedown());

Upvotes: 7

Related Questions