Last active
February 19, 2023 08:43
-
-
Save TekExplorer/406a8c6903b767feb334da69022863e1 to your computer and use it in GitHub Desktop.
A gist containing examples for copyWith code that can allow passing in null to a copy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
print('Wont pass null into a copy'); | |
final e1 = Example1(id: 1, name: 'hello 1'); | |
final e1Copy = e1.copyWith(name: null); | |
print('e1: ${e1.id}, ${e1.name}'); | |
print('e1Copy: ${e1Copy.id}, ${e1Copy.name}'); | |
print('\nSubclassing method'); | |
final e2 = Example2(id: 2, name: 'hello 2'); | |
final e2Copy = e2.copyWith(name: null); | |
print('e2: ${e2.id}, ${e2.name}'); | |
print('e2Copy: ${e2Copy.id}, ${e2Copy.name}'); | |
print('\nCallable class method'); | |
final e4 = Example4(id: 4, name: 'hello 4'); | |
final e4Copy = e4.copyWith(name: null); | |
print('e4: ${e4.id}, ${e4.name}'); | |
print('e4Copy: ${e4Copy.id}, ${e4Copy.name}'); | |
} | |
// wont pass null into a copy | |
class Example1 { | |
const Example1({ | |
required this.id, | |
this.name, | |
}); | |
final int id; | |
final String? name; | |
Example1 copyWith({ | |
int? id, | |
String? name, | |
}) { | |
return Example1( | |
id: id ?? this.id, | |
name: name ?? this.name, | |
); | |
} | |
} | |
const SENTINAL = Object(); | |
// subclassing method | |
abstract class Example2 { | |
factory Example2({required int id, String? name}) = Example2Impl; | |
const Example2._({ | |
required this.id, | |
this.name, | |
}); | |
final int id; | |
final String? name; | |
Example2 copyWith({ | |
int? id, | |
String? name, | |
}); | |
} | |
class Example2Impl extends Example2 { | |
Example2Impl({ | |
required super.id, | |
super.name, | |
}) : super._(); | |
@override | |
Example2 copyWith({ | |
Object? id = null, | |
Object? name = SENTINAL, | |
}) { | |
return Example2Impl( | |
id: id == null ? this.id : id as int, | |
name: name == SENTINAL ? this.name : name as String?, | |
); | |
} | |
} | |
// callable class method | |
class Example4 { | |
const Example4({ | |
required this.id, | |
this.name, | |
}); | |
final int id; | |
final String? name; | |
Example4CopyWith get copyWith => Example4CopyWith(this); | |
} | |
abstract class Example4CopyWith { | |
factory Example4CopyWith(Example4 value) = Example4CopyWithImpl; | |
Example4 call({ | |
int? id, | |
String? name, | |
}); | |
} | |
class Example4CopyWithImpl implements Example4CopyWith { | |
const Example4CopyWithImpl(this._value); | |
final Example4 _value; | |
@override | |
Example4 call({ | |
Object? id = null, | |
Object? name = SENTINAL, | |
}) { | |
return Example4( | |
id: id == null ? _value.id : id as int, | |
name: name == SENTINAL ? _value.name : name as String?, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment