此函数的返回类型为

此函数的返回类型为

我有警告:“此函数的返回类型为'FutureOr ',但不以return语句结尾。”
我的密码

  Future<List<Task>> getAllTasks() async {
    _readTaskList().then((dynamic value) {
      if (value != null) {
        final List<Task> tasks = <Task>[];
        final List<Task> _tasks = value as List<Task>;
        tasks.forEach((dynamic element) {
          _tasks.add(_convertFromJsonToTask(element as Map<String, dynamic>));
        });
        return tasks;
      } else {
        return null;
      }
    });
  }

最佳答案

尝试执行以下操作:

Future<List<Task>> getAllTasks() async {
  final dynamic value = await _readTaskList();

  if (value != null) {
    final List<Task> tasks = <Task>[];
    final List<Task> _tasks = value as List<Task>;
    tasks.forEach((dynamic element) {
      _tasks.add(_convertFromJsonToTask(element as Map<String, dynamic>));
    });
    return tasks;
  } else {
    return null;
  }
}
或者,如果您真的想使用then:
Future<List<Task>> getAllTasks() {
  return _readTaskList().then((dynamic value) {
    if (value != null) {
      final List<Task> tasks = <Task>[];
      final List<Task> _tasks = value as List<Task>;
      tasks.forEach((dynamic element) {
        _tasks.add(_convertFromJsonToTask(element as Map<String, dynamic>));
      });
      return tasks;
    } else {
      return null;
    }
  });
}

关于flutter - 此函数的返回类型为,但不以return语句结尾,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63170987/

10-09 06:56