How do I supply/set an initial value to a text field in Flutter ?

By Using TextEditingController we can set initial value to Text filed.

class LandingPage extends StatefulWidget{
  @override
  State createState() =>LandingPageState();

}

class LandingPageState  extends State {
  TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = new TextEditingController
(text: 'Enter Values');
  }

  @override
  Widget build(BuildContext context) {
    return new Column(
      children: [
        new TextField(
          controller: _controller,
        ),
      ],
    );
  }
}

 (OR)

By Using  TextFormField

class LandingPageState extends State {
  TextEditingController _controller;

  @override void initState() {
    super.initState();
    _controller = new TextEditingController (text: 'Enter Values');
  }

  @override Widget build(BuildContext context) {
    return new Column(children: [ 
TextFormField(
      autofocus: false,
      initialValue: 'your initial text',
      decoration: InputDecoration(
        hintText: 'Last Name',
      ),
    )
],);
  }
}
 

 


Subscribe For Daily Updates

Flutter Questions
Android Questions