我已经设置了AutoCompleteTextField,但是当我也通过发送和接收相同的输入值从一页导航到另一页时,该值不会显示在AutoCompleteTextField中。而且,当我点击建议时,我遇到了错误
码:
class CustomRegistration extends StatefulWidget {
final String pincodeC;
CustomRegistration([this.pincodeC])
@override
_CustomRegistrationState createState() => _CustomRegistrationState();
}
class _CustomRegistrationState extends State<CustomRegistration> {
FocusNode pincodeFocus = new FocusNode();
TextEditingController pincode = new TextEditingController();
List<String> suggestionList = ['170010','110011','170001'];
SimpleAutoCompleteTextField textField;
@override
Widget build(BuildContext context) {
Column body = new Column(children: [
new ListTile(
title: textField,)
]);
body.children.addAll(added.map((item) {
return new ListTile(title: new Text(item));
}));
print(textField);
return Scaffold(
body: Builder(
builder: (context) => SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AutoCompleteTextField(
focusNode: pincodeFocus,
decoration: InputDecoration(
border: InputBorder.none,
prefixIcon: Icon(Icons.home),
hintText: "Pincode",
),
controller: pincode,
itemSubmitted: (item) {
setState(() {
textField.controller.text = item.toString();
print('item submitted' + item.toString());
});
},
key: key,
suggestions: suggestionList,
itemBuilder: (context, item){
return Container(
padding: EdgeInsets.all(20.0),
child: Row(
children: <Widget>[
Text(
item,
style: TextStyle(color: Colors.black),
)
],
),
);
},
itemSorter: (a, b) {
return a.compareTo(b);
},
itemFilter: (item, query) {
return item
.toLowerCase()
.toString()
.startsWith(query.toLowerCase());
},
),
]))));}}
当我打印文本字段时,我得到 null 以及在哪里初始化小部件。pincodeC? 最佳答案
您需要将构造函数对象定义为textcontroller。您需要设置pincodeC Controller 。
textController pincodeC;
- - - - - - - - - - - - - - - - - -和 - - - - - - - ...... AutoCompleteTextField(
controler:pincodeC,
)
关于flutter - 刷新页面时AutoCompleteTextField检索null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64781189/