谁能在DART中解释使用零秒级联的使用?

发布于 2025-01-28 11:44:46 字数 198 浏览 2 评论 0 原文

我对文档所说的内容感到困惑:

”文档代码屏幕截图

I'm confused about what the documentation says here:

documentation code screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2025-02-04 11:44:46

示例类

class Foo {
  String? text;
  List<String> classes = [];

  @override
  String toString() {
    return "text $text classLength ${classes.length}";
  }
}

QuerySelector 可以返回null而不是 foo

Foo? querySelector({required bool retunNull}) {
  return retunNull ? null : Foo();
}
void main(List<String> args) {
  /// `querySelector` can return  null Foo
  Foo? foo = querySelector(retunNull: true);

  // foo can be null, that's we cant directly use `text`
  // if we use `!`, it means we are sure that `foo` is not null.
  // but we cant be sure because `querySelector` can return null,
  // therefore we use `?` means, if left side is null simply retun exit from this expression
  foo?.text = "Confirm";

  //same here,
  foo?.classes.add("cls");

  /// now our `foo` object will contain `text=Confirm` and a class `c1`
  ///* only if `foo` isnot null
  print(foo);// print null

  /// Now the short version
  /// we use `..` to perform multiple operation on single line
  /// while the `querySelector` can retun null
  ///  we need to use `?..` means exit the expression if `querySelector` retun null.
  Foo? foo1 = querySelector(retunNull: true)
    ?..text = "Confirm1"
    ..classes.add("cls1");

  print(foo1);// print null

  Foo? foo2 = querySelector(retunNull: false)
    ?..text = "Confirm2"
    ..classes.add("cls2");

  print(foo2);// print `text Confirm2 classLength 1`
}

Example class

class Foo {
  String? text;
  List<String> classes = [];

  @override
  String toString() {
    return "text $text classLength ${classes.length}";
  }
}

querySelector can return null instead of Foo

Foo? querySelector({required bool retunNull}) {
  return retunNull ? null : Foo();
}
void main(List<String> args) {
  /// `querySelector` can return  null Foo
  Foo? foo = querySelector(retunNull: true);

  // foo can be null, that's we cant directly use `text`
  // if we use `!`, it means we are sure that `foo` is not null.
  // but we cant be sure because `querySelector` can return null,
  // therefore we use `?` means, if left side is null simply retun exit from this expression
  foo?.text = "Confirm";

  //same here,
  foo?.classes.add("cls");

  /// now our `foo` object will contain `text=Confirm` and a class `c1`
  ///* only if `foo` isnot null
  print(foo);// print null

  /// Now the short version
  /// we use `..` to perform multiple operation on single line
  /// while the `querySelector` can retun null
  ///  we need to use `?..` means exit the expression if `querySelector` retun null.
  Foo? foo1 = querySelector(retunNull: true)
    ?..text = "Confirm1"
    ..classes.add("cls1");

  print(foo1);// print null

  Foo? foo2 = querySelector(retunNull: false)
    ?..text = "Confirm2"
    ..classes.add("cls2");

  print(foo2);// print `text Confirm2 classLength 1`
}

Run on dartPad

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文