Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 502
Norbert Avatar asked Sep 23 '18 13:09

Norbert


1 Answers

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());
like image 175
Günter Zöchbauer Avatar answered Sep 30 '22 23:09

Günter Zöchbauer