Skip to content

Instantly share code, notes, and snippets.

@xros
Last active December 26, 2021 14:52
Show Gist options
  • Save xros/3c65583640000df2b1bf6060a6944f1b to your computer and use it in GitHub Desktop.
Save xros/3c65583640000df2b1bf6060a6944f1b to your computer and use it in GitHub Desktop.
simple copyWith method in Dart
// simple copyWith method
class Credentials {
const Credentials({this.email = '', this.password = ''});
final String email;
final String password;
Credentials copyWith({String? email, String? password}) {
return Credentials(
email: email ?? this.email,
password: password ?? this.password,
);
}
@override
String toString() => "Credentials $email, $password";
}
void main() {
final cred = Credentials();
print(cred);
final newCred = cred.copyWith(email: '[email protected]');
print(newCred);
final newCred2 = newCred.copyWith(password: 'asdfsf');
print(newCred2);
}
@xros
Copy link
Author

xros commented Dec 26, 2021

output

Credentials , 
Credentials [email protected], 
Credentials [email protected], asdfsf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment