Home > other >  How to make a textEditingController global
How to make a textEditingController global

Time:08-17

I want to make a global TextEditingController final TextEditingController _notes = TextEditingController(); and want to use in other files. is there any way to do that? It is not working globally as I do for other variables. for each file(where I need to use) I have to declare again.

CodePudding user response:

final TextEditingController notes = TextEditingController();

Declare this outside only one class in which you are using this or declare it outside main.dart. And call it where ever you want to call in any class.

Imp Point:

final TextEditingController _notes = TextEditingController();

Adding ( _ ) before any variable makes it private in dart.

CodePudding user response:

Try this

class GlobalVar {
  GlobalVar._();
  static final textEditingController = TextEditingController();
}

Widget someWidget() {
  return TextFormField(
    controller: GlobalVar.textEditingController,
  );
}
  • Related