Reputation: 5
I have warning: "This function has a return type of 'FutureOr<List>', but doesn't end with a return statement."
My code
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;
}
});
}
Upvotes: 0
Views: 637
Reputation: 7660
The issue is that the value from _readTaskList()
does not get returned before the getAllTasks()
function is completely executed. Simply replacing .then
with await
will fix this problem
Future<List<Task>> getAllTasks() async {
var 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;
}
}
Upvotes: 2
Reputation: 31199
Try do this instead:
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;
}
}
Or if you really want to use 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;
}
});
}
Upvotes: 0