简体   繁体   English

参数类型 &#39;Future<xxx> &#39; 不能分配给参数类型 &#39;FutureOr<xxx> &#39; 颤振和飞镖

[英]The argument type 'Future<xxx>' can't be assigned to the parameter type 'FutureOr<xxx>' Flutter & Dart

This code works if I use String as the type, but not if I use a class.如果我使用 String 作为类型,则此代码有效,但如果我使用类则无效。

Calling code:调用代码:

Future<Card> getCard() {
    return Future.value(currPack.getCard());
  }

Here is the getCard method:这是 getCard 方法:

  Future<Card> getCard() async {
    return Card("a", "b");
  }

Here is the Card class:这是卡片类:

class Card  {
  String question = "";
  String answer = "";

  Card(String question, String answer) {
    this.question = question;
    this.answer = answer;
  }
}

When I use the Card class, I get a warning message saying:当我使用 Card 类时,我收到一条警告消息:

The argument type 'Future<Card>' can't be assigned to the parameter type 'FutureOr<Card>'参数类型 'Future<Card>' 不能分配给参数类型 'FutureOr<Card>'

If I change the return type from Card to String and set the method to return a string it works fine.如果我将返回类型从 Card 更改为 String 并将方法设置为返回一个字符串,它工作正常。

Turns out there is a class in material design called Card: 'package:flutter/src/material/card.dart'.原来材料设计中有一个类叫做 Card:'package:flutter/src/material/card.dart'。

The first bit of code is part of a screen that imports material.dart (import 'package:flutter/material.dart').第一段代码是导入 material.dart 的屏幕的一部分(import 'package:flutter/material.dart')。

The other code is from a standalone class that doesn't import material.dart, but does reference the local card class (import 'package:flashcards/modules/Card.dart';).另一个代码来自一个独立的类,它不导入 material.dart,但引用了本地卡片类(import 'package:flashcards/modules/Card.dart';)。

So to sum it up: the error was caused by having references to two classes with the same name and not distinguishing between the two.所以总结一下:错误是由于引用了两个同名的类而没有区分这两个类引起的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 错误:参数类型 'Future<object?> Function()' 不能赋值给参数类型 'FutureOr<dynamic> 功能(动态)'</dynamic></object?> - Flutter error: The argument type 'Future<Object?> Function()' can't be assigned to the parameter type 'FutureOr<dynamic> Function(dynamic)' 如何解决参数类型'Future<file> 功能(地图<string, dynamic> )' 不能分配给参数类型'FutureOr<file> 在 Flutter</file></string,></file> - How to solve The argument type 'Future<File> Function(Map<String, dynamic>)' can't be assigned to the parameter type 'FutureOr<File> in Flutter Flutter升级后:参数类型'Future<bool> 函数(文件)'不能分配给参数类型'FutureOr<dynamic> 函数(Uint8List)'</dynamic></bool> - After Flutter upgrade: The argument type 'Future<bool> Function(File)' can't be assigned to the parameter type 'FutureOr<dynamic> Function(Uint8List)' flutter / dart 错误:参数类型 'Future<file> ' 不能分配给参数类型 'File'</file> - flutter / dart error: The argument type 'Future<File>' can't be assigned to the parameter type 'File' 参数类型 &#39;xxx&#39; 不能分配给参数类型 &#39;Widget? 函数(BuildContext?,地图<String, List<String> &gt;)&#39; - The argument type 'xxx' can't be assigned to the parameter type 'Widget? Function(BuildContext?, Map<String, List<String>>)' Flutter: 参数类型 'Future<bool?> Function()' 不能分配给参数类型 'Future<bool> 功能()?'</bool></bool?> - Flutter: The argument type 'Future<bool?> Function()' can't be assigned to the parameter type 'Future<bool> Function()?' 参数类型 &#39;Future<Widget> &#39; 不能分配给参数类型 &#39;Widget&#39;.dart - The argument type 'Future<Widget>' can't be assigned to the parameter type 'Widget'.dart 参数类型“对象?” 不能分配给参数类型 'FutureOr<uint8list> ?'</uint8list> - The argument type 'Object?' can't be assigned to the parameter type 'FutureOr<Uint8List>?' 错误:无法将参数类型“void Function(MidiDevice)”分配给参数类型“FutureOr”<dynamic> 函数(列表<mididevice> ?)'</mididevice></dynamic> - Error: The argument type 'void Function(MidiDevice)' can't be assigned to the parameter type 'FutureOr<dynamic> Function(List<MidiDevice>?)' 参数类型“字符串函数(字符串)”不能分配给参数类型“FutureOr”<string> 功能(电话号码)'</string> - The argument type 'String Function(String)' can't be assigned to the parameter type 'FutureOr<String> Function(PhoneNumber)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM