Getter _text isn't defined for class TagColumn in Flutter

576

dart treates variables that start with an underscore as a private variable (since there is no private keyword in dart) so in order to solve your probelm, you need to remove the _(underscore) before the text variable.

what ill do is this

1- move the _text variable to the TagColumn class insted of the State class

class TagColumn extends StatefulWidget{
 final text = TextEditingController(); // removed the _ so that to access it inside the Practise class
  @override
  State<StatefulWidget> createState() => new _TagColumn();
}

and update the TagColumn class to reflect those changes


class _TagColumn extends State<TagColumn>{
   // final _text = TextEditingController(); <---- since the text is now in the TagColumn class not the state class
  bool _validate = false;

  @override

  Widget build(BuildContext context){
    final tagField = TextField(
      controller: widget.text,
      obscureText: false,
      style: TextStyle(fontFamily: 'Play', color: Colors.white, fontSize: 20),
      maxLines: null,
      keyboardType: TextInputType.text,
      decoration: InputDecoration(
        contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
        hintText: "Tag",
          errorText: _validate ? 'Value Can\'t be Empty': null,
          border:
          OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
      );
    return Container(
      width: MediaQuery.of(context).size.width/2 - 40,
      margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
      decoration: BoxDecoration(
        color: Colors.blue,
        borderRadius: BorderRadius.circular(32.0),
      ),
      child: Theme(
        data: ThemeData(
          hintColor: Colors.white,
        ),
        child: tagField,
      ),
    );
  }
}
Share:
576
Ashu Mundra
Author by

Ashu Mundra

Updated on December 20, 2022

Comments

  • Ashu Mundra
    Ashu Mundra over 1 year

    I have looked at this question on Stack Overflow Flutter getter isn't specified for the class, when it is specified. And I still cannot understand why my class Practice does not have access to the variable _text which is accessed from an element in the List with type TagColumn.

    class Practice extends StatefulWidget {
      @override
      _PracticeState createState() => _PracticeState();
    }
    
    class _PracticeState extends State<Practice>{
      int count  = 0;
    
      @override
      Widget build(BuildContext context){
        List<TagColumn> ok = List.generate(count, (int i) => new TagColumn());
        return Scaffold(
          backgroundColor: Colors.black,
          body: new LayoutBuilder(builder: (context, constraint){
          return new Stack(
            children: <Widget>[
              SingleChildScrollView(
                child: SafeArea(
                  child: new Wrap(
                    direction: Axis.horizontal,
                    children: ok,
                  )
                ),
              ),
              new Positioned(
                child: new Align(
                  alignment: FractionalOffset.bottomRight,
                  child: Container(
                    margin: EdgeInsets.only(bottom: 50.0, right: 40.0),
                    child: RawMaterialButton(
                      onPressed: (){
                        setState(() {
                          if(count != 0 && ok[count]._text.text.isEmpty){
    
                          }
                          else{
                              count +=1;
                          }
                        });
                      },
                      shape: CircleBorder(),
                      child: Icon(
                        Icons.add_circle,
                        size: 100.0,
                        color: Color(0xffd3d3d3),
                      ),
                    )
                  )
                )
              )
    
            ],
          );
          }),
        );
      }
    }
    
    class TagColumn extends StatefulWidget{
      @override
      State<StatefulWidget> createState() => new _TagColumn();
    }
    
    class _TagColumn extends State<TagColumn>{
      final _text = TextEditingController();
      bool _validate = false;
    
      @override
    
      Widget build(BuildContext context){
        final tagField = TextField(
          controller: _text,
          obscureText: false,
          style: TextStyle(fontFamily: 'Play', color: Colors.white, fontSize: 20),
          maxLines: null,
          keyboardType: TextInputType.text,
          decoration: InputDecoration(
            contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
            hintText: "Tag",
              errorText: _validate ? 'Value Can\'t be Empty': null,
              border:
              OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
          );
        return Container(
          width: MediaQuery.of(context).size.width/2 - 40,
          margin: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
          decoration: BoxDecoration(
            color: Colors.blue,
            borderRadius: BorderRadius.circular(32.0),
          ),
          child: Theme(
            data: ThemeData(
              hintColor: Colors.white,
            ),
            child: tagField,
          ),
        );
      }
    }
    

    What I am trying to do is not allow the user to create a new tag when pressing, "Plus," in the bottom right corner(see the image below) if the user doesn't enter text in the current one. In other words, if it's not empty. Thus, I am using the variable final _text = TextEditingController(), to check if the current tag is empty when pressing the plus button. If not, a new tag is created.

    enter image description here