flutter analyze does not detect returns from switch statement with all enum cases

Given code like this:

enum MyEnum { Val1, Val2 }


  Widget buildListItem() {
    switch (item.type) {
      case MyEnum.Val1:
        return Container();
      case MyEnum.Val2:
       return Container();

flutter analyze complains:

Analyzing ZZZZZ-app...                                           

   info • This function has a return type of 'Widget', but doesn't end with a return statement • lib/ZZZZZ.dart:16:3 •

Now if I add a default case:

        return Container(); // just to satisfy flutter analyzer

analyzer is happy.

Given that the VS Code plugin is able to warn me when I have missing cases in the switch statement based on the known values of the enum and then be ok once I have cases covering both values without a default case, I would expect that analyzer would be able to know this too?

Author: Fantashit

2 thoughts on “flutter analyze does not detect returns from switch statement with all enum cases

  1. Also, just FYI, you don’t need a break after a return. The list of statements following a case is required to either be empty, or to end with return, throw, rethrow, break or continue.

  2. @QuintinWillison your code is exactly the same as mine when I opened this issue and you’re making the exact same mistake I did, which as Vyacheslav quite rightly pointed out to me: you are not allowing for the fact that your state can have a value of null.

    Also I’m not sure why you consider this surprising compared to Java given Java’s behaviour is very similiar and if anything more surprising.

    Given that null-safety feature is being added to Dart literally as we speak, if anything this is just a temporary inconvenience.

Comments are closed.