2

I have a lot of Text Editing Controller and instead of repeating it and writing it again and again, is there a way to create a list and assign it to a controller?

TextEditingController _Length =  TextEditingController();
TextEditingController _Width =  TextEditingController();

TextEditingController _LengthInch =  TextEditingController();
TextEditingController _WidthInch =  TextEditingController();

TextEditingController _LengthFt =  TextEditingController();
TextEditingController _WidthFt =  TextEditingController();

TextEditingController doorLength = TextEditingController();
TextEditingController doorWidth = TextEditingController();

TextEditingController doorLengthFt = TextEditingController();
TextEditingController doorWidthFt = TextEditingController();

TextEditingController doorLengthInch = TextEditingController();
TextEditingController doorWidthInch = TextEditingController();

TextEditingController tileLength = TextEditingController();
TextEditingController tileWidth = TextEditingController();

TextEditingController tileLengthInch = TextEditingController();
TextEditingController tileWidthInch = TextEditingController();

I use these text editing controllers in different text field form.

any help is highly appreciated.

Ahmad
  • 95
  • 6

1 Answers1

1
    // Your controller Identity Name
    enum YourSpecialControllerKeysNameForClass {
      _Length,
      _Width,
      _LengthInch,
      _WidthInch,
      _LengthFt,
      _WidthFt,
      doorLength,
      doorWidth,
      doorLengthFt,
      doorWidthFt,
      doorLengthInch,
      doorWidthInch,
      tileLength,
      tileWidth,
      tileLengthInch,
      tileWidthInch,
    }
    
    
    
      // Create each for controller names
  Map<YourSpecialControllerKeysNameForClass, TextEditingController> createTextControllers() {
    Map<YourSpecialControllerKeysNameForClass, TextEditingController> createdList = {};

    createdList.addEntries(YourSpecialControllerKeysNameForClass.values.map((e) => MapEntry<YourSpecialControllerKeysNameForClass, TextEditingController>(e, TextEditingController())).toList());

    return createdList;
  }
    
      // Get Controllers By Name
      TextEditingController getTextController(YourSpecialControllerKeysNameForClass key) {
        return controllerList.entries.toList().firstWhere((element) => element.key == key).value;
      }
    
      // And Define this any Widget  
      Map<YourSpecialControllerKeysNameForClass, TextEditingController> controllerList = {};
    
    
      // Initialize youR contollers in initState
      initState() {
    
        // Create Contoller
        controllerList = createTextControllers();
      }
    
    
    
    
      // Usage
      getTextController(YourSpecialControllerKeysNameForClass._Length);




///
///// If you send data to the list then only what you send is disposed.
////  If you don't, they are all disposed.
///
void disposeAllController({List<YourSpecialControllerKeysNameForClass>? onlyList}) {
  if (onlyList != null) {
    controllerList.entries.where((element) => onlyList.contains(element.key)).toList().map((e) => e.value.dispose());
  } else {
    controllerList.values.map((e) => e.dispose());
  }
}
rasityilmaz
  • 764
  • 1
  • 6
  • 11
  • Hi, Thanks you soooooo much for the help, I really appreciate it. I have copied you code and replaced the names but it throws an error saying, **The method 'toHashMap' isn't defined for the type 'List'. Try correcting the name to the name of an existing method, or defining a method named 'toHashMap'.** A little more help please. Thanks – Ahmad Nov 13 '22 at 20:11
  • Okey, use toMap() instead of toHashMap() – rasityilmaz Nov 13 '22 at 20:13
  • bro. same error **toMap()** is also not working – Ahmad Nov 13 '22 at 20:23
  • It really work. Thank you soooooo much. I really really appreciate your time and help. I am a civil engineer and has 90% developed an application to speed up the engineering calculations. I am doing it all by myself and the app works fine when i test it in my mobile. I have learnt everything from internet and people like you, make it possible for us noob to learn and stay motivated. – Ahmad Nov 13 '22 at 20:47
  • Please!, glad I was able to help – rasityilmaz Nov 13 '22 at 22:27
  • bro, can you please tell me how to dispose the whole list at once. or i should dispose them one by one in dispose call. – Ahmad Nov 16 '22 at 17:57
  • Sure,you can recheck my answer – rasityilmaz Nov 16 '22 at 18:32
  • again. Thank you soooo much for the help. I really appreciate it. – Ahmad Nov 16 '22 at 20:26