[英]Flutter error: The argument type 'Future<Object?> Function()' can't be assigned to the parameter type 'FutureOr<dynamic> Function(dynamic)'
[英]The argument type 'Future<xxx>' can't be assigned to the parameter type 'FutureOr<xxx>' Flutter & Dart
如果我使用 String 作為類型,則此代碼有效,但如果我使用類則無效。
調用代碼:
Future<Card> getCard() {
return Future.value(currPack.getCard());
}
這是 getCard 方法:
Future<Card> getCard() async {
return Card("a", "b");
}
這是卡片類:
class Card {
String question = "";
String answer = "";
Card(String question, String answer) {
this.question = question;
this.answer = answer;
}
}
當我使用 Card 類時,我收到一條警告消息:
參數類型 'Future<Card>' 不能分配給參數類型 'FutureOr<Card>'
如果我將返回類型從 Card 更改為 String 並將方法設置為返回一個字符串,它工作正常。
原來材料設計中有一個類叫做 Card:'package:flutter/src/material/card.dart'。
第一段代碼是導入 material.dart 的屏幕的一部分(import 'package:flutter/material.dart')。
另一個代碼來自一個獨立的類,它不導入 material.dart,但引用了本地卡片類(import 'package:flashcards/modules/Card.dart';)。
所以總結一下:錯誤是由於引用了兩個同名的類而沒有區分這兩個類引起的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.