Home > OS >  The switch case expression type 'String' must be a subtype of the switch expression type &
The switch case expression type 'String' must be a subtype of the switch expression type &

Time:09-15

I put a switch case for each of my dropdown values, its show the alert The switch case expression type 'String' must be a subtype of the switch expression type 'SelectFieldBloc<String, dynamic>'. so what can i do to change it into String, dynamic. This is my switch case

    switch(_formBloc){
      case 'Option 1': return Text("zero widget");
      case 1: return Text("one widget");
      case 2: return Text("two widget");
      case 3: return Text("three widget");
      case 4: return Text("three widget");
      case 5: return Text("three widget");
      default: return Text("default widget");
    }

and this is where i store the value of my drop down:

final filepicker2 = InputFieldBloc<PlatformFile?, Object>(initialValue: null);

CodePudding user response:

I'm not familiar with bloc so I might be wrong, but I'm guessing you want to switch on the value and not the bloc. Also the numbers are int and not String so you might want to write '1' instead of 1. So like:

switch(_formBloc.value){
  case 'Option 1': return Text("zero widget");
  case '1': return Text("one widget");
  case '2': return Text("two widget");
  case '3': return Text("three widget");
  case '4': return Text("three widget");
  case '5': return Text("three widget");
  default: return Text("default widget");
}

CodePudding user response:

 switch(_formBloc){
      case 0: return Text("zero widget");
      case 1: return Text("one widget");
      case 2: return Text("two widget");
      case 3: return Text("three widget");
      case 4: return Text("three widget");
      case 5: return Text("three widget");
      default: return Text("default widget");
    }

prefer to use only one datatype in case

  • Related