gpt4 book ai didi

flutter - dart中构造函数中的括号是什么

转载 作者:行者123 更新时间:2023-12-03 03:01:14 30 4
gpt4 key购买 nike

@immutable
abstract class MyGithubReposState extends Equatable {
MyGithubReposState([List props = const []]) : super(props);
}

我在我使用的库之一中看到了上面的代码。 [List props = const []] 有什么用意思? Prop list 一览?

最佳答案

这是可选参数,如下所述。

  • 一个函数可以有两种类型的参数:
    需要 可选 .

  • 首先列出必需的参数,然后是任何可选参数。可选参数可以是命名的或位置的。
  • 可选参数可以是命名的或位置的,但不能两者兼而有之。

  • 命名参数
    调用函数时,您可以使用 paramName: value 指定命名参数。例如:

    this is calling of function

    enableFlags(bold: true, hidden: false);
    定义函数时,使用 {param1, param2, ...} 指定命名参数:

    this is how we define them

    /// Sets the [bold] and [hidden] flags ...
    void enableFlags({bool bold, bool hidden}) {...}
    位置参数
    在 [] 中包装一组函数参数将它们标记为可选的位置参数:
    String say(String from, String msg, [String device]) {
    var result = '$from says $msg';
    if (device != null) {
    result = '$result with a $device';
    }
    return result;
    }

    so that we can call this function by two way


    没有可选的位置参数
    say('Bob', 'Howdy')
    带有可选的位置参数
    say('Bob', 'Howdy', 'smoke signal')
    Reference here

    关于flutter - dart中构造函数中的括号是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59259913/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com