我正在用 postgresql 做 dart,我无法返回 conn.query(rows) 的数据。但是结果来了,如何返回,简单的代码是
main(){
someOtherFunc();
}
Future add() async{
var uri = 'postgres://postgres:root@localhost:5432/testdb';
var conn = await connect(uri);
var sql = 'select * from test';
return conn.query(sql).toList();
}
Future someOtherFunc() async {
print(await add());
}
我得到的返回是“‘_Future’的实例!”
最佳答案
如果您在调用 add()
的函数中使用 await
,您将获得所需的结果
Future add() async {
var uri = 'postgres://postgres:root@localhost:5432/testdb';
var conn = await connect(uri);
var sql = 'select * from test';
return conn.query(sql).toList();
}
Future someOtherFunc() async {
print(await add());
}
async
具有传染性。当您调用异步函数时,您无法返回同步执行。所有依赖于异步结果的东西,都需要使用 await
(或“旧的” .then((value) {})
)。 async
+ await
只是创造同步执行的错觉。
关于postgresql - 如何从 dart 中的 conn.query 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34611764/