在我的 flutter Api 中,我有如下代码,但 lint 2.0.1 警告我:Navigator.pushnamed(context
不要跨异步间隙使用 BuildContext
这个SO有一些信息,但如果我尝试使用该解决方案,我会收到一个错误,mounted is undefined。我不知道如何按照下面的答案的建议将我的 Api 类转换为 StatefulWidget。
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;
}
}
Run Code Online (Sandbox Code Playgroud) flutter ×1