skip to Main Content

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.

2

Answers


  1. 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();
      }
    }
    
    Login or Signup to reply.
  2. A variable with a name starting with an underscore (_) is private to its library. You can only access it in the file where it is defined.

    Name the variable firstNameCtrlr, without underscore, and import the file where it is defined into the file where you want to use it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search