相关疑难解决方法(0)

异步等待 Navigator.push() - 出现 linter 警告:use_build_context_synchronously

在 Flutter 中,所有Navigator将新元素推送到导航堆栈的函数都会返回 a Future,因为调用者可以等待执行并处理结果。

我大量使用它,例如将用户(通过push())重定向到新页面时。当用户完成与该页面的交互时,我有时还希望原始页面pop()

onTap: () async {
  await Navigator.of(context).pushNamed(
    RoomAddPage.routeName,
    arguments: room,
  );

  Navigator.of(context).pop();
},
Run Code Online (Sandbox Code Playgroud)

一个常见的示例是使用带有敏感操作(例如删除实体)的按钮的底部工作表。当用户单击该按钮时,将打开另一个底部工作表,要求确认。当用户确认时,确认对话框以及打开确认底部表单的第一个底部表单将被关闭。

所以基本上onTap底部工作表内的“删除”按钮的属性如下所示:

onTap: () async {
  bool deleteConfirmed = await showModalBottomSheet<bool>(/* open the confirm dialog */);
  if (deleteConfirmed) {
    Navigator.of(context).pop();
  }
},
Run Code Online (Sandbox Code Playgroud)

这种方法一切都很好。我遇到的唯一问题是 linter 发出警告:use_build_context_synchronouslyBuildContext ,因为我在函数完成后使用相同的警告async

我忽略/暂停此警告是否安全?但是,我如何使用相同的后续代码等待导航堆栈上的推送操作BuildContext?有合适的替代方案吗?一定有可能做到这一点,对吧?

PS:我不能也不想检查该mounted属性,因为我没有使用StatefulWidget.

asynchronous lint dart flutter flutter-navigation

53
推荐指数
2
解决办法
2万
查看次数

不要跨异步间隙使用 BuildContexts flutter

我已经创建了一个注册函数,但收到一条警告,Do not use BuildContexts across async gaps.提示Utils.flushBarErrorMessage("No Internet", context);我是 flutter 新手,想知道如何使用asyncawait

Future _registration() async {
    String name = _nameController.text.trim();
    String email = _emailController.text.trim();
    String password = _passwordController.text.trim();
    String phone = _phoneController.text.trim();

    if (name.isEmpty) {
      Utils.flushBarErrorMessage("Type your name", context);
    } else if (email.isEmpty) {
      Utils.flushBarErrorMessage("Type your email", context);
    } else if (!GetUtils.isEmail(email)) {
      Utils.flushBarErrorMessage("Type valid email address", context);
    } else if (password.isEmpty) {
      Utils.flushBarErrorMessage("Type your password", context);
    } else if (password.length < 6) …
Run Code Online (Sandbox Code Playgroud)

async-await dart flutter

6
推荐指数
1
解决办法
7648
查看次数

如何在异步方法中调用snackbar?

在这里,当您在异步内部调用 Snackbar 时,通常您可能会注意到警告或错误,指出不要跨异步间隙使用 BuildContexts。如何在类对象中而不是在小部件树中避免这种情况

flutter

4
推荐指数
1
解决办法
2183
查看次数

Flutter - 不要跨异步间隙使用 BuildContext

在我的 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

3
推荐指数
1
解决办法
6234
查看次数

尝试在颤振中跨异步间隙使用构建上下文

我正在尝试构建一个聊天应用程序,并将此类称为 ChatBubble。这还能够从主题列表中进行分类。这是我的代码:

class _ChatBubbleState extends State<ChatBubble> {
  final topicsDb = TopicsDatabase();
  // Copy text to clipboard
  void _copyToClipboard(String text) {...

  final GlobalKey _key = GlobalKey();

  @override
  Widget build(BuildContext context) {
    //bool isDarkMode = Theme.of(context).brightness == Brightness.dark;
    // get the Topic object using the topicId of the note
    Topic? topic = widget.note.topicId != null
        ? topicsDb.getTopicById(widget.note.topicId!)
        : null;

    double maxWidth = MediaQuery.of(context).size.width * 0.7; //Trying to dynamically change the size and failing. 
    return Container(
      key: _key,
      padding: const EdgeInsets.only(left: 14, right: …
Run Code Online (Sandbox Code Playgroud)

asynchronous dart flutter

2
推荐指数
1
解决办法
415
查看次数