Android TextField中输入的文本未添加到列表中

Android TextField中输入的文本未添加到列表中,android,flutter,Android,Flutter,我正在创建一个应用程序,用户在其中的文本字段中输入一些文本。有3个文本字段。我希望在文本字段中输入的输入文本存储在列表中。我试图通过运行下面的代码来解决这个问题。但它总是输出一个空列表。似乎找不到问题 class _ListPageState extends State<ListPage> { List<String> items = []; var counter = 0; @override Widget build(BuildContext co

我正在创建一个应用程序,用户在其中的文本字段中输入一些文本。有3个文本字段。我希望在文本字段中输入的输入文本存储在列表中。我试图通过运行下面的代码来解决这个问题。但它总是输出一个空列表。似乎找不到问题

class _ListPageState extends State<ListPage> {

  List<String> items = [];
  var counter = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Create Your Need List', style: TextStyle(color: Colors.grey[500]),), backgroundColor: Colors.white, elevation: 0,),
      body: Padding(
        padding: const EdgeInsets.fromLTRB(15, 25, 15, 0),
        child: Column(
          children: <Widget>[
            TextField(
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                  hintText: 'Add item'
              ),
              onChanged: (String str){
                setState(() {
                  items[counter] = str;
                  counter +=1;
                });
              },
            ),
            TextField(
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                  hintText: 'Add item'
              ),
              onChanged: (String str){
                setState(() {
                  items[counter] = str;
                  counter +=1;
                });
              },
            ),
            TextField(
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                  hintText: 'Add item'
              ),
              onChanged: (String str){
                setState(() {
                  items[counter] = str;
                  counter +=1;
                });
              },
            ),
            SizedBox(height: 10,),
          ],
        ),
      ),
    floatingActionButton: FloatingActionButton(
    onPressed: () {
      Navigator.pushAndRemoveUntil(context, SlideFromLeft(widget: Dashboard()), ModalRoute.withName('/dashboard'));
      print(items);
    },
    child: Icon(Icons.navigate_next),
    backgroundColor: Colors.pink,
    )
    );
  }
}
class\u ListPageState扩展状态{
列表项=[];
var计数器=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:Text(“创建您的需求列表”),样式:TextStyle(颜色:Colors.grey[500]),背景色:Colors.white,标高:0,),
主体:填充物(
填充:LTRB(15,25,15,0)中的常量边集,
子:列(
儿童:[
文本字段(
装饰:输入装饰(
边框:OutlineInputBorder(),
hintText:“添加项目”
),
onChanged:(字符串str){
设置状态(){
项目[柜台]=str;
计数器+=1;
});
},
),
文本字段(
装饰:输入装饰(
边框:OutlineInputBorder(),
hintText:“添加项目”
),
onChanged:(字符串str){
设置状态(){
项目[柜台]=str;
计数器+=1;
});
},
),
文本字段(
装饰:输入装饰(
边框:OutlineInputBorder(),
hintText:“添加项目”
),
onChanged:(字符串str){
设置状态(){
项目[柜台]=str;
计数器+=1;
});
},
),
尺寸箱(高度:10,),
],
),
),
浮动操作按钮:浮动操作按钮(
已按下:(){
Navigator.pushandremoveintil(上下文,SlideFromLeft(小部件:Dashboard()),ModalRoute.withName('/Dashboard');
印刷品(项目);
},
子:图标(图标。导航到下一步),
背景颜色:Colors.pink,
)
);
}
}

您没有将添加到列表中

您需要更改
项目[计数器]=str

list.add(str);

我想你们可能想用这个列表作为字典。你也可以这样做

  List<String> items = ['', '', ''];
列表项=['','';
而不是

  List<String> items = [];
List items=[];

只是初始化一个空列表。同时修改计数器+=1;到固定索引。

您直接在某个索引处添加值,而不定义列表大小。使用
items.add(value)
将项目添加到列表中并初始化为`list items=list()`