I have a list of Strings (called namesList). For each String inside the list I want to create a button with one of the names.
As you can see in the code below, I have tried to make use of the for loop. However, when playing the code I can only see the first Button with the text "Anna".
import 'package:flutter/material.dart';
class ButtonsWithName extends StatefulWidget{
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _ButtonsWithNameState();
}
}
class _ButtonsWithNameState extends State<ButtonsWithName> {
String name1;
String name2;
String name3;
String name4;
String name5;
var namesList = new List<String>();
@override
void initState() {
name1 = 'Anna';
name2 = 'Bernd';
name3 = 'Christina';
name4 = 'David';
name5 = 'Elena',
namesList.add(name1);
namesList.add(name2);
namesList.add(name3);
namesList.add(name4);
namesList.add(name5);
super.initState();
}
Widget _buildButtonsWithNames() {
for(int i=0; i < namesList.length; i++){
RaisedButton(child: Text(answerList[0]));
}
return RaisedButton[i];
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Wrap(children: <Widget>[
_buildButtonsWithNames();
]);
)
}
}
I expect to have 5 RaisedButtons in the end with the texts of the String-list, namely a button with the text "Anna", a button with the text "Bernd", and so and so forth. I would really I appreciate anyone's help on this matter!