flutterdarterror-handlingcompiler-errorsstatic-analysis

The error handler of Future.catchError must return a value of the future's type


This gives a runtime error.

Future<int> nuclearPlant() async {
  await Future.delayed(Duration(seconds: 1));
  throw "up";
  return 999999999;
}
Future<void> main() async {
  final zillionVolts = await nuclearPlant().catchError((e) => "0");
  print(zillionVolts);
  print("Done, safely shutdown the plant.");
}

The error handler of Future.catchError must return a value of the future's type

I agree with the error. But why is it a runtime ?? Not a compiler error? How to enable a linter analysis check to prevent a code containing this ?

In critical code example above, someone mistakenly put 0 as String during migration 2.0. As a result, the plant power plant did not shut down causing massive destruction because Dart allowed this code to run in production.


Solution

  • Here is the rule that you need: https://dart.dev/tools/diagnostic-messages?utm_source=dartdev&utm_medium=redir&utm_id=diagcode&utm_content=invalid_return_type_for_catch_error#invalid_return_type_for_catch_error

    It has warning level by default, here is how you enforce it to be error:

    analyzer:
      errors:
        invalid_return_type_for_catch_error: error
    

    Important note: you must run dart analyze on your code, because rule itself will not break flutter build or flutter run command. Based on that, you actually has 2 options:

    1. Enforce this rule to error, like I said before, and then run dart analyze in your CI pipeline
    2. Do not make any changes in your analysis_options.yaml, and run dart analyze --fatal-infos --fatal-warnings – that is how you will get cleanest code.

    I personally prefer second option.