[英]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.