Flutter TextFormField 如何设置初始值的问题

class _FooState extends State<Foo> {
  TextEditingController _controller;
 
  @override
  void initState() {
    super.initState();
//setState很重要不然数据不会更新!!!
    setState((){
      _controller = new TextEditingController(text: '初始值');
     }
);  
} @override Widget build(BuildContext context) { return new Column( children: <Widget>[ new TextField( //设置controller, controller: _controller, ), new RaisedButton( onPressed: () { //清除输入 _controller.clear(); }, child: new Text('清除'), ), ], ); } }

注意在初始化的时候,setState一定要调用,不然数据不会正常更新!

原文地址:https://www.cnblogs.com/fengfenghuifei/p/13453225.html