In my flutter Api, I have code like the following but lint 2.0.1 warns me at Navigator.pushnamed(context
Do not use BuildContext across async gaps
This SO has some information but I get an error that mounted is undefined if I try to use that solution. I can't figure out how to convert my Api class to a StatefulWidget as suggested by the answer below.
class Api {
Future<List<Author>> getAuthors(BuildContext context) async {
List<Author> authors = [];
try {
final response = await _helper.get(context, "/authors");
if (response.statusCode == 200) {
var parsed = json.decode(response.body);
if (parsed is List<dynamic>) {
for (var author in parsed) {
authors.add(Author.fromJson(author));
}
}
} else {
Navigator.pushNamed(context, RoutePaths.login);
return authors;
}
} catch (e) {
return authors;
}
return authors;
}
}