1

In the following code, I have implemented a textfield and its value will be stored by _getVin. But I am receiving null value before user is able to enter a value. So what will be a correct solution ? What are the changes to be made?

 @override
  Widget build(BuildContext context) {
    TextEditingController _getVin = TextEditingController();
     var vin =  VIN(number: _getVin.text, extended: true);


return new Scaffold (

             body:  Container(
                padding: new EdgeInsets.only(top: 16.0),
              child: TextField(
                controller: _getVin,
                decoration:InputDecoration(
                  border :OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(5.0)),
                      borderSide: BorderSide(color:Colors.black)
                  ),
                  labelText: "Enter VIN",
                  labelStyle: TextStyle(
                      fontSize: 15,
                      color:Colors.black
                  ),
                ),

              ),
            ),
               GestureDetector(
                   onTap:() {
                     print(_getVin);

                     print(vin.modelYear());

                         },


                child:Card(
                 child: Text( "Press Here"),
                                    )
                 ),
ashish rana
  • 185
  • 7

1 Answers1

0

Oops, you should not be creating controllers inside a build method, you will be effectively creating a new one each time flutter rebuilds that widget, and you won't be disposing it. The correct approach is to use a Stateful widget:

class VinWidget extends StatefulWidget {
  const VinWidget({Key? key}) : super(key: key);

  @override
  _VinWidgetState createState() => _VinWidgetState();
}

class _VinWidgetState extends State<VinWidget> {
  late TextEditingController _getVin = TextEditingController();

  @override
  Widget build(BuildContext context) {
    
 return  Scaffold (

             body:  Container(
                padding:  EdgeInsets.only(top: 16.0),
              child: TextField(
                controller: _getVin,
                decoration:InputDecoration(
                  border :OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(5.0)),
                      borderSide: BorderSide(color:Colors.black)
                  ),
                  labelText: "Enter VIN",
                  labelStyle: TextStyle(
                      fontSize: 15,
                      color:Colors.black
                  ),
                ),

              ),
            ),
               GestureDetector(
                   onTap:() {
var vin = VIN(number: _getVin.text, extended: true);
                     print(_getVin);

                     print(vin.modelYear());

                         },


                child:Card(
                 child: Text( "Press Here"),
                                    )
                 ),
  }

  @override
  void dispose() {
    super.dispose();
    _getVin.dispose();
  }
}

croxx5f
  • 5,163
  • 2
  • 15
  • 36