Search code examples
androidflutterdarttexteditingcontroller

Using the variable in TextEditingController in another dart file


I am trying to use the variable name _firstNameCtrlr in another page or rather class file. File1.dart

final _firstNameCtrlr = TextEditingController();

File2.dart

class TxtFrmFldCtrlr{
  static void clearField(){
    _firstNameCtrlr.clear();
  }
}

The reason why I am trying to separate this function is that so I could put them all in one file, however the I cannot access the variable name of TextEditingController.

I tried importing the File1.dart in File2.dart but it did not fixed it.


Solution

  • You can access it by passing the widgets controller in clearField function.

    import 'package:flutter/cupertino.dart';
    
    class TxtFrmFldCtrlr{
      static void clearField(TextEditingController _firstNameCtrlr){
        _firstNameCtrlr.clear();
      }
    }