Home > Net >  Switch fallthrough all cases bellow the selected case in Flutter/Dart
Switch fallthrough all cases bellow the selected case in Flutter/Dart

Time:02-28

I am developing an industrial app using flutter.
The app involves lot of input from user be it TextFormField or DropDownSearchField.

If user changes/modifies the data/value of any field that is above current field all the data of bellow field must be cleared.

e.g. : If user has set 10 of 12 field numbered (1 to 12), then user decides to change Field 3 then all field bellow Field 3 must be cleared (reset).

I was looking into Switch fallthrough but dart seems to give me error The 'case' should not complete normally. Try adding 'break', or 'return', etc.

I really want to avoid code duplication where by I write clear code in each case. Using if statement is complicating logic.
I looked into solution but I wish to avoid label and continue statement (as this look like goto statement from other languages).

What are my options here?

Code :

void clearDataAfterStepChange({required int step}) {
    switch (step) {
      //Clear All Steps Bellow 1 -> Product Code
      case 1:
        _productCodeController.clear();
      //Clear All Steps Bellow 2 -> Batch Number
      case 2:
        _batchNumberController.clear();
      //Clear All Steps Bellow 3 -> Area
      case 3:
        _areaController.clear();
      //Clear All Steps Bellow 4 -> Room Name
      case 4:
        _roomNumberController.clear();
      //Clear All Steps Bellow 5 -> Stage
      case 5:
        _stageController.clear();
      //Clear All Steps Bellow 6 -> Product Type
      case 6:
        _productTypeController.clear();
      //Clear All Steps Bellow 7 -> Product Name
      case 7:
        _productNameController.clear();
      //Clear All Steps Bellow 8 -> Product Version Number
      case 8:
        _productVersionNumberController.clear();
      //Clear All Steps Bellow 9 -> Version Number
      case 9:
        _versionNumberController.clear();
      //Clear All Steps Bellow 10 -> BMR Number
      case 10:
        _bmrNumberController.clear();
      //Clear All Steps Bellow 11 -> BMR Version Number
      case 11:
        _bmrVersionNumberController.clear();
      //Clear All Steps Bellow 12 -> Process Order Number
      case 12:
        _processOrderNumberController.clear();
      // Do Nothing
      default:
        break;
    } 
}

CodePudding user response:

There's nothing wrong with using continue LABEL. That's what it's there for. (There's also nothing inherently wrong with using goto in C either, and in some common situations it's good form, but I digress.)

However, in your specific case where each step invokes a single zero-argument function1, you could create a List of callbacks of each of the steps, and then just iterate over them and invoke them:

void clearDataAfterStepChange({required int step}) {
  var steps = <void Function(void)>[
    _productCodeController.clear,
    _batchNumberController.clear,
    _areaController.clear,
    _roomNumberController.clear,
    _stageController.clear,
    _productTypeController.clear,
    _productNameController.clear,
    _productVersionNumberController.clear,
    _versionNumberController.clear,
    _bmrNumberController.clear,
    _bmrVersionNumberController.clear,
    _processOrderNumberController.clear,
  ];
  
  for (var i = step - 1; i >= 0 && i < steps.length; i  = 1) {
    steps[i]();
  }
}

1 You could still do it even if each step were more complex too, but it would be uglier.

CodePudding user response:

Forget switch statement, it won't solve this problem. So try this way :

 void clearDataAfterStepChange({required int step}) {
     Map<int, TextEditingController> textControllerMap = {
    11: _processOrderNumberController,
    10: _bmrVersionNumberController,
    9: _bmrNumberController,
    8: _versionNumberController,
    7: _productVersionNumberController,
    6: _productNameController,
    5: _productTypeController,
    4: _stageController,
    3: _roomNumberController,
    2: _areaController,
    1: _batchNumberController,
  };

  for (int i = 1; i <= textControllerMap.length; i  ) {
    if (step <= i) {
      textControllerMap[i].clear();
    }
  }
}
  • Related