Reputation: 183
how to implement when i am press button 'changenName', all of added and displayed name will change to 'test2'? I have to use nameList.forEach() or nameList.where()?
String name = 'test';
List<String> nameList = List<String>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Center(
child: nameList.length == 0
? Text('0')
: Column(
children: nameList
.map(
(e) => Text(e),
)
.toList(),
),
),
RaisedButton(
onPressed: () {
setState(() {
nameList.add(name);
});
},
child: Text('add'),
),
RaisedButton(
onPressed: () {
setState(() {
//??
});
},
child: Text('changeName'),
)
],
),
Upvotes: 1
Views: 2317
Reputation: 342
You can write a function for updating the name which takes a name to be changed as an argument and onPressed you can call that function inside setState.
After changing the name once and for further addition if you want the same name to be added then you might have to assign that String to the variable 'name';
Example:
changeName(String name){
for(int i=0;i<list.length;i++){
list[i] = name;
}
}
String name = 'test';
List<String> nameList = List<String>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Center(
child: nameList.length == 0
? Text('0')
: Column(
children: nameList
.map(
(e) => Text(e),
)
.toList(),
),
),
RaisedButton(
onPressed: () {
setState(() {
nameList.add(name);
});
},
child: Text('add'),
),
RaisedButton(
onPressed: () {
setState(() {
name = 'test2'; //If you want to change the names for future
//values
changeName(name);
});
},
child: Text('changeName'),
)
],
),
Upvotes: 1