Dart Future.then返回意外的空值
以下方法打印存储在Redis中的密钥的值Dart Future.then返回意外的空值,dart,future,dart-async,Dart,Future,Dart Async,以下方法打印存储在Redis中的密钥的值 Future getValue(字符串连接字符串,字符串键){ 返回RedisClient.connect(connectionString).then((RedisClient){ 然后((值)=>print(值)); }); } 但是,当我尝试从main()打印值时,getValue方法打印null而不是值(如上所述的方法): 库持久化; Future getValue(字符串连接字符串、字符串键){ 返回RedisClient.connect(c
Future getValue(字符串连接字符串,字符串键){
返回RedisClient.connect(connectionString).then((RedisClient){
然后((值)=>print(值));
});
}
但是,当我尝试从main()打印值时,getValue方法打印null而不是值(如上所述的方法):
库持久化;
Future getValue(字符串连接字符串、字符串键){
返回RedisClient.connect(connectionString).then((RedisClient){
client.get(key);
});
}
-
import'package:test/persistence.dart'作为持久性;
主要(){
persistence.getValue(
“本地主机:6379/0”,
“钥匙”)
.然后((值)=>打印(值));//**打印空值…为什么**
}
似乎.then((value)=>print(value))
执行得太早了
有人能帮我吗?在
客户端之前添加一个return
。get(key)
或使用简短的方法表单=>client.get(key)
那么你不需要return你只需要使用。然后(打印)
并尝试在client.get(key)中添加一个return
代码>。嗨,罗伯特,你说得对。在client.get(key)级别添加return解决了这个问题。非常感谢:)太好了!!短方法“=>”使其更干净:返回RedisClient.connect(connectionString).then((RedisClient-client)=>client.get(key));非常感谢:)