Reputation: 1
I'm creating an app where the user enters some text in Text Fields. There are 3 Text Fields. I want the input text that's being entered in the Text Fields to be stored in a list. I'm trying to to this by running the code below. But it always outputs an empty list. Can't seem to find the problem.
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,
)
);
}
}
Upvotes: 0
Views: 45
Reputation: 890
I thought you might want to use List as dictionary. You also can do like that
List<String> items = ['', '', ''];
rather than
List<String> items = [];
just init an empty List. At the same time modify counter +=1; to fixed index.
Upvotes: 1
Reputation: 14135
You are not adding into the list.
You need change items[counter] = str;
to
list.add(str);
Upvotes: 0