Future.then返回意外的空值

时间:2015-03-22 13:14:41

标签: dart future dart-async

以下方法打印存储在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))执行得太早。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:2)

return之前添加client.get(key)或使用简短方法表单=> client.get(key),然后您就不需要return