使用模式匹配,很容易创建如下条件:

if (json case {"data": final Map data, "changed": final List changed}
        when changed.isNotEmpty) {
  ... perform some work with 'data' and 'changed'.
}

但是,如果要应用多个模式,这会导致代码不可读(就嵌套而言).

我想做的是有模式保护条款,这样我就可以写成这样:

if (NOT json case {"data": final Map data, "changed": final List changed}
        when changed.isNotEmpty) {
  return;    
}
... perform some work with 'data' and 'changed'.

推荐答案

https://dart.dev/language/pattern-types列出了DART支持的模式类型,没有提到逻辑非模式.

但是,你仍然可以通过以下方式避免pyramid of doom分:

if (json case {"data": final Map data, "changed": final List changed}
        when changed.isNotEmpty) {
} else {
  return;
}

// ... perform some work with 'data' and 'changed'.

Dart相关问答推荐

当 ng-repeat 嵌套在 AngularDart 中时访问外部 $index

Flutter.io (dart) - 如何创建左填充覆盖?

在 Dart 中处理字节数组时,使用 Uint8List 优于 List 有什么优势?

Flutter - 可以在没有 Firebase 的情况下使用 Google 登录吗?

在 Dart 中将对象推入数组

如何在Flutter应用程序中使用套接字?

Flutter url_launcher 未在发布模式下启动 url

从Dart中的另一个文件导入扩展名方法

如何仅从 DateTime.now() 中提取时间;

如何更改手机屏幕上的Flutter应用程序名称显示?

'dart:async' 的函数 `runZoned` 的用途

断言失败时如何在 Dart 中打印消息?

如何在 Visual Studio Code 中禁用fake右括号注释?

如何从列表中的元素创建逗号分隔的字符串

在 Dart 中将 List 转换为字符串?

VS Code 首选项的位置

如何在抽象类中声明工厂构造函数?

如何在 Dart 中尚不存在的目录 struct 中创建文件?

使用 Dart 将 Uint8List 转换为字符串

在 Dart 中没有换行符的 print()?