Home > front end >  Using the variable in TextEditingController in another dart file
Using the variable in TextEditingController in another dart file

Time:12-06

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.

CodePudding user response:

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();
  }
}

CodePudding user response:

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.

  • Related