Can‘t use the default List constructor.

博客指出在Flutter中不能使用默认List构造函数,原因是Dart的“Null Safety”安全检查使变量不能为“null”值。还提到在Flutter2.2后,new List()方法已弃用,并给出了替换方案,介绍了固定长度和可变长度列表的创建方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Can’t use the default List constructor.

在这里插入图片描述

这是由于 Dart 中包含了一项名为“Null Safety”的安全检查,该功能使得变量不能具有“null”值。

解决方案:

void main() {
  var list = []..add(3)..add(4);
  print(list);
}

在Flutter2.2后,new List()方法已弃用

在这里插入图片描述

替换方案

List<String> list = [];

List<String> list = List.empty(growable: true);

growable 为 false 是为 固定长度列表,为 true 是为 长度可变列表

创建给定长度的列表

要创建具有给定长度的可增长列表,对于可为空元素类型,只需在创建后立即分配长度:

void main() {
    List<dynamic> list = []..length = 3;
    list[1]=3;
    list[0]='li';
    print(list.length);
    print(list);
}

在这里插入图片描述

对于不可为空的元素类型,替代方法如下:

List<int> growableList = List<int>.filled(3, 0, growable: true);
List<String> list = List<String>.filled(3, "", growable: true);

在这里插入图片描述

```python import arch # 使用help函数查看arch.arch_model的文档 help(arch.arch_model) ``` 输出结果为: ``` Help on function arch_model in module arch.univariate.mean: arch_model(y, x=None, mean='Constant', lags=0, vol='Garch', p=1, o=0, q=1, power=2.0, dist='Normal', hold_back=None, rescale=False, **kwargs) Construct a new ARCHModel instance using the provided specification. Parameters ---------- y : array_like The dependent variable x : array_like, optional Exogenous regressors. Ignored if model does not permit exogenous regressors. mean : str, optional Name of the mean model. Currently supported options are: 'Constant', 'Zero', 'AR', 'ARX', 'HAR', 'HARX', 'LS', 'GLS', 'ARMAX', 'HARMAX', 'CustomMean'. Default is 'Constant'. lags : int or list[int], optional Either a scalar integer value indicating lag length or a list of integers specifying lag locations. Used in the construction of the selected mean model. Default is 0. vol : str, optional Name of the volatility model. Currently supported options are: 'Garch', 'ConstantVariance', 'EWMAVariance', 'HARCH', 'Constant', 'EGARCH', 'FIGARCH', 'ARCH', 'TGARCH', 'GJR-GARCH', 'AVARCH', 'NAGARCH', 'MidasRegression', 'MidasVariance', 'CustomVolatility'. Default is 'Garch'. p : int, optional Order of the symmetric innovation. Used in the construction of the selected volatility model. Default is 1. o : int, optional Order of the asymmetric innovation. Used in the construction of the selected volatility model. Default is 0. q : int, optional Order of lagged volatility terms. Used in the construction of the selected volatility model. Default is 1. power : float, optional Power to use in the case of an ARCH in mean model. Default is 2.0. dist : str, optional Name of the distribution. Currently supported options are: 'Normal', 'StudentsT', 'SkewStudent', 'GED', 'Exponential', 'Beta', 'GeneralizedPareto', 'Gamma', 'LogNormal', 'Kernel'. Default is 'Normal'. hold_back : {None, int}, optional Integer offset from the start of the sample at which to begin fitting the model. Used to allow estimation of models with lags that cannot be included at the beginning of the sample. Default is None. rescale : bool, optional Flag indicating to rescale the data to improve optimization. Default is False. **kwargs Additional keyword arguments to pass to the mean or volatility model constructor. Returns ------- model : ARCHModel Configured ARCHModel instance. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gavi曦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值