i have a question. i have this class:
class TableWidget extends StatefulWidget {
final String table_number;
const TableWidget({Key? key, required this.table_number}) : super(key: key);
@override
State<TableWidget> createState() => _TableWidget();
}
class _TableWidget extends State<TableWidget> {
Color? iconcolor = Colors.black;
void checkstate(){
print(i);
for (var j = 0; j < i; j ){
if(lines[j].substring(0, 7) == widget.table_number){
setState(() {
iconcolor = Colors.red;
});
}
}
}
@override
Widget build(BuildContext context){
return Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
// Some code
],
);
}
}
Now, i have multiple copies of the TableWidget class, and i have them in a List , to be precise, 9 of them:
List<TableWidget> tables = [
TableWidget(table_number: "Table 1"),
TableWidget(table_number: "Table 2"),
TableWidget(table_number: "Table 3"),
TableWidget(table_number: "Table 4"),
TableWidget(table_number: "Table 5"),
TableWidget(table_number: "Table 6"),
TableWidget(table_number: "Table 7"),
TableWidget(table_number: "Table 8"),
TableWidget(table_number: "Table 9"),
];
How can i call the checkstate() function for each one of the 9 instances of the class, outside of the class, say in main();
Thanks a lot.
CodePudding user response:
well if the List tables is global then you can do a simple for loop. something like:
for(var table in tables){
table.checkstate()}
CodePudding user response:
Try adding it in the first class TableWidget instead of _TableWidget which is a stateful class. It allows you to access the forEach feature in other classes.
Something like this:
class TableWidget extends StatefulWidget {
final String table_number;
static Color? iconcolor = Colors.black;
void checkstate(int i){
print(i);
for (var j = 0; j < i; j ){
if(lines[j].substring(0, 7) == widget.table_number){
iconcolor = Colors.red;
}
}
}
const TableWidget({Key? key, required this.table_number}) : super(key: key);
@override
State<TableWidget> createState() => _TableWidget();
}
class _TableWidget extends State<TableWidget> {
@override
Widget build(BuildContext context){
return Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
// Some code
],
);
}
}