Flutter 在构造函数内调用异步函数
我有一个小部件,其中一个属性是通过查询数据库获得的。所以这个函数必须标记为异步,但我不能在构造函数中调用它。有人知道如何解决这个问题吗Flutter 在构造函数内调用异步函数,flutter,asynchronous,dart,constructor,Flutter,Asynchronous,Dart,Constructor,我有一个小部件,其中一个属性是通过查询数据库获得的。所以这个函数必须标记为异步,但我不能在构造函数中调用它。有人知道如何解决这个问题吗 class PositionWidget extends StatefulWidget { final String streetName; String date; List<Map> notification; final VoidCallback onDelete; PositionWidget({Key key,
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;
}
class PositionWidget扩展了StatefulWidget{
最后一个字符串streetName;
字符串日期;
名单通知;
最终删除;
PositionWidget({Key,this.streetName,this.date,this.notification,@required this.onDelete}):超级(Key:Key){
this.notification=wait DBHelper.instance.getNotification(this.streetName);
}
}
未来getNotification(字符串街道)异步{
Database db=await DBHelper.instance.Database;
var res=await db.query(表,列:[columnNot],其中:“${DBHelper.columnName}=?”,其中rgs:[street]);
返回res;
}
您不能有一个异步
构造函数。使用FutureBuilder
可能是最好的解决方案
或者,您可以像这样等待结果:
PositionWidget({Key key, this.streetName, this.date,this.notification, @required this.onDelete}): super(key: key){
DBHelper.instance.getNotification(this.streetName)
.then((result){
notification = result;
});
}
您不能有
async
构造函数。使用FutureBuilder
可能是最好的解决方案
或者,您可以像这样等待结果:
PositionWidget({Key key, this.streetName, this.date,this.notification, @required this.onDelete}): super(key: key){
DBHelper.instance.getNotification(this.streetName)
.then((result){
notification = result;
});
}
在
StatefulWidget
class PositionWidgetState extends State<PositionWidget> {
var res;
@override
void initState() {
res = getNotification(widget.streetName);
}
您需要在PositionWidgetState
的initState()方法中进行调用(您的PositionWidget的State
)
class PositionWidgetState extends State<PositionWidget> {
var res;
@override
void initState() {
res = getNotification(widget.streetName);
}
类位置WidgetState扩展状态{
var-res;
@凌驾
void initState(){
res=getNotification(widget.streetName);
}
您可能需要一个FutureBuilder或StreamBuilder来处理结果您不能在StatefulWidget的构造函数中执行此操作
class PositionWidgetState extends State<PositionWidget> {
var res;
@override
void initState() {
res = getNotification(widget.streetName);
}
initState() {
myAsyncInit();
}
myAsyncInit() async {
//do async stuff
}
您需要在PositionWidgetState
的initState()方法中进行调用(您的PositionWidget的State
)
class PositionWidgetState extends State<PositionWidget> {
var res;
@override
void initState() {
res = getNotification(widget.streetName);
}
类位置WidgetState扩展状态{
var-res;
@凌驾
void initState(){
res=getNotification(widget.streetName);
}
您可能需要使用FutureBuilder或StreamBuilder来处理结果我认为您应该根据从db获取的方法使用Future或Stream builder。我认为您应该根据从db获取的方法使用Future或Stream builder。虽然此代码可能会解决问题,但一个好的答案也应该解释代码的内容虽然这段代码可能会解决这个问题,但一个好的答案也应该解释代码的作用以及它是如何帮助的。FutureBuilder
是一个与任何其他小部件一样的小部件。它正在监听未来并给出它的多个状态。查看以获取更多信息当连接状态为sn时,我应该返回什么样的小部件apshot未“完成”?例如,您可以返回CircularProgressIndicator
,或任何其他您想要的小部件,如文本文本(“加载”)
,一个空的容器
。FutureBuilder
与其他小部件一样,是一个小部件。它正在监听未来并给出它的多种状态。查看以获取更多信息当快照的连接状态未“完成”时,我应该返回什么样的小部件?例如,您可以返回循环压缩器指示器
,也可以返回您想要的任何其他小部件,如文本文本(“加载”)
、空容器
。。。
initState() {
myAsyncInit();
}
myAsyncInit() async {
//do async stuff
}