以下方法将打印存储在Redis中的键的值
Future<String> getValue(String connectionString, String key) {
return RedisClient.connect(connectionString).then((RedisClient client) {
client.get(key).then((value) => print(value));
});
}
但是当我尝试从main()打印值时,getValue方法将输出null而不是值(如上述方法):
library persistence;
Future<String> getValue(String connectionString, String key) {
return RedisClient.connect(connectionString).then((RedisClient client) {
client.get(key);
});
}
--
import 'package:test/persistence.dart' as persistence;
main () {
persistence.getValue(
"localhost:6379/0",
"key")
.then((value) => print(value)); // **prints null.... WHY???**
}
似乎
.then((value) => print(value))
执行得太早了。有谁能够帮助我?
最佳答案
在return
之前添加client.get(key)
或使用简短方法形式=> client.get(key)
,则不需要return
。
关于dart - Future.then返回意外的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29194827/