Flutter - type 'Future<dynamic>' is not a subtype of type 'FutureOr<String>'

Viewed 2061

I'm getting this error: type 'Future<dynamic>' is not a subtype of type 'FutureOr<String>'

void main() async {
  hello();
}

Future<String> hello() async {
  return dynamicHello(); // error : type 'Future<dynamic>' is not a subtype of type 'FutureOr<String>'
  return dynamicHello().then((value) => value); // working well
}

Future<dynamic> dynamicHello() async {
  return 'HELLO';
}

can someone please explain me the difference between return dynamicHello() and return dynamicHello().then((value) => value) above code

I checked the signature on Future is Future then(FutureOr action(T value), {Function onError}). However, I did not clearly understand why "return dynamicHello()" works normally if I make Future<String> dynamicHello().

1 Answers

You must return both Future are String or dynamic. try below code hope its help to you.

Refer Future class here

Both are String

void main() async {
  hello();
}

Future<String> hello() async {
  return dynamicHello(); // happen : type 'Future<dynamic>' is not a subtype of type 'FutureOr<String>'
  return dynamicHello().then((value) => value); // working well
}

Future<String> dynamicHello() async {
  return 'HELLO';
}

Or Both are dynamic

void main() async {
  hello();
}

Future<dynamic> hello() async {
  return dynamicHello(); // happen : type 'Future<dynamic>' is not a subtype of type 'FutureOr<String>'
  return dynamicHello().then((value) => value); // working well
}

Future<dynamic> dynamicHello() async {
  return 'HELLO';
}

or

void main() async {
  hello();
}

Future  hello() async {
  return dynamicHello(); // happen : type 'Future<dynamic>' is not a subtype of type 'FutureOr<String>'
  return dynamicHello().then((value) => value); // working well
}

Future dynamicHello() async {
  return 'HELLO';
}
Related