【问题标题】:Dart Error: Unhandled exception: E/flutter ( 5079): Invalid argument: Instance of 'Future<String>'Dart 错误:未处理的异常:E/flutter(5079):无效参数:'Future<String>' 的实例
【发布时间】:2019-02-27 05:20:38
【问题描述】:

我正在尝试将文档添加到我的云 Firestore 数据库。以这种方式。

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());

但是,每当我运行代码时,我都会收到此错误。我需要有关如何成功解决此问题的帮助

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)

【问题讨论】:

  • 如果您的代码格式正确,您的代码会更容易理解。 VSCode 和 IntelliJ 能够格式化 Dart 代码。你也可以使用dartpad.dartlang.org

标签: firebase dart firebase-authentication flutter google-cloud-firestore


【解决方案1】:

currentlyIn 返回一个Future。你需要这样对待它。 Future 不会自动转换为完成时的值。

您可以使用async/await 喜欢:

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());

【讨论】:

    【解决方案2】:

    您正在尝试使用 Future&lt;String&gt; 对象作为 String 因此它给出了错误。试试下面的代码。

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

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 2020-09-03
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 2020-07-26
      • 2021-10-04
      • 2021-01-03
      • 1970-01-01
      相关资源
      最近更新 更多