简体   繁体   中英

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.

Calling code:

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

Here is the getCard method:

  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:

The argument type 'Future<Card>' can't be assigned to the parameter type 'FutureOr<Card>'

If I change the return type from Card to String and set the method to return a string it works fine.

Turns out there is a class in material design called 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').

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';).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM