1

The Fat Arrow is syntactic sugar, like described here

But if I remove return in(look for: future.then(print); // << Removed return) then it complains about missing return?:

Hmm, or am I missing a "return-return" kind og thing...

import 'dart:async';

main() {
  printsome();
  print("After should be called first...");
}

Future<void> printsome() {
  final future = delayedFunction();
  future.then(print); // << Removed return
  // You don't *have* to return the future here.
  // But if you don't, callers can't await it.
}

const news = '<gathered news goes here>';
const oneSecond = Duration(seconds: 1);
Future<String> delayedFunction() =>
    Future.delayed(oneSecond, () => news);

I get this warning:

[dart] This function has a return type of 'Future<void>', but doesn't end with a return statement. [missing_return]
Chris G.
  • 23,930
  • 48
  • 177
  • 302

1 Answers1

1

The => expr implicitly returns the result of expr, so if you want to replace it with a function body { return expr; } you need to add return, otherwise null will be implicitly returned.

You declared a function with return type Future<void> and DartAnalyzer warns you that your function is not returning such a value.

You can either add async which makes the function implicitly return a Future

Future<void> printsome() async {
  final result = await delayedFunction();
}

or if you don't want to use async you can just add return

Future<void> printsome() {
  final future = delayedFunction();
  return future.then(print); // return the Future returned by then()
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567