gpt4 book ai didi

flutter - 有没有办法在 Dart 中克​​隆对象?

转载 作者:行者123 更新时间:2023-12-05 01:08:48 24 4
gpt4 key购买 nike

我在 Stackoverflow 上遇到了一些关于此的问题,但对我来说没有任何意义。最简单的方法是什么?

最佳答案

查看以下类(class)以供引用:

class Customer {
final String id;
final String name;
final String address;
final String phoneNo;
final String gstin;
final String state;

Customer({
this.id = '',
@required this.name,
@required this.address,
@required this.phoneNo,
this.gstin,
@required this.state,
});

Customer copyWith({
String name,
String address,
String phoneNo,
String gstin,
String state,
}) {
return Customer(
name: name ?? this.name,
address: address ?? this.address,
phoneNo: phoneNo ?? this.phoneNo,
gstin: gstin ?? this.gstin,
state: state ?? this.state,
);
}

}

使用 copyWith 构造函数可以创建对象的副本。

如果你不将任何参数传递给 copyWith 构造函数,它会返回具有相同值的相同对象

但是,如果您希望更改使用 copyWith 构造函数所做的任何参数,它将返回带有您传递的新参数值的对象副本

注意:在 copyWith 构造函数中,假设如果您更改了一个参数值,那么另一个参数值与第一个对象保持相同。

关于flutter - 有没有办法在 Dart 中克​​隆对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65699710/

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