I need to make a dynamic list of Widgets
Then I created a function that returns a list of widgets
@override
Widget build(BuildContext context) {
return Container(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Column(
children: getRandomWidgetArray(),
),
),
),
);
}
List<Widget> getRandomWidgetArray() {
List<Widget> widgets = [];
for (int i = 0; i < 5; i++) {
widgets.add(
TextFormField(
decoration: const InputDecoration(
labelText: 'DEP',
labelStyle: TextStyle(
color: Colors.black,
fontSize: 12,
fontWeight: FontWeight.bold,
),
fillColor: Colors.black12,
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(4),
),
),
),
),
);
}
return widgets;
}
In this format it returns the data I need
But if I put a function to fetch the data from the fields in the database, it returns an error.
`_TypeError (type ‘Future<dynamic>’ is not a subtype of type ‘List<dynamic>’)
getEnsaioItem(String ens_codigo, String ens_estaca) async {
try {
final Database db = await _getDatabase();
List ensaioItem = [];
ensaioItem = await db.rawQuery(
"SELECT * FROM ensaioitem WHERE ens_codigo = ? AND ens_ite_estaca = ? ORDER BY ens_ite_ordem ASC",
[ens_codigo, ens_estaca]);
return ensaioItem.toList();
} catch (ex) {
return List.empty();
}
}
```
Function that should fetch the data
```
List<Widget> getRandomWidgetArray() {
List itemEnsaio = [];
itemEnsaio = EnsaioItemDatasource().getEnsaioItem('210', '1');
for (int i = 0; i < itemEnsaio.length; i++) {
print(itemEnsaio[i]['ens_codigo']);
}
}
```
New contributor
Mateus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.