I have a widget where an attribute is obtained through a query on a database. So this function has to be marked as asynchronous, but i can't call it inside a constructor. Does someone know how to solve this ?
class PositionWidget extends StatefulWidget {
final String streetName;
String date;
List<Map> notification;
final VoidCallback onDelete;
PositionWidget({Key key, this.streetName, this.date,this.notification, @required this.onDelete}): super(key: key){
this.notification = await DBHelper.instance.getNotification(this.streetName);
}
}
Future<List<Map>> getNotification(String street) async {
Database db = await DBHelper.instance.database;
var res = await db.query(table, columns: [columnNot], where: '${DBHelper.columnName} = ?', whereArgs: [street]);
return res;
}
You can't do this inside the constructor of your StatefulWidget
You need to make your call inside the the initState()
method of the PositionWidgetState
(the State
of your PositionWidget
class PositionWidgetState extends State<PositionWidget> {
var res;
@override
void initState() {
res = getNotification(widget.streetName);
}
You'll probably need to a FutureBuilder or StreamBuilder to deal with the result
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments