Created
July 12, 2022 14:08
-
-
Save maheshj01/77849fc2ba23c946af106f30b9a45ae2 to your computer and use it in GitHub Desktop.
CopyWith isn't working
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
import 'package:flutter/foundation.dart'; | |
void main() { | |
final newWord = Word('123','hello','greet'); | |
newWord.copyWith(id: '345',word:"edited word"); | |
print(newWord.id); // `123` instead of `345` | |
print(newWord.word); // `hello` instead of `edited word` | |
} | |
class Word { | |
final String id; | |
String word; | |
String meaning; | |
List<String>? synonyms; | |
List<String>? examples; | |
List<String>? mnemonics; | |
DateTime? created_at; | |
Word(this.id, this.word, this.meaning, | |
{this.synonyms = const [], | |
this.mnemonics = const [], | |
this.created_at, | |
this.examples = const []}); | |
/// TODO: Doesn't seem to work for iterables | |
Word copyWith( | |
{String? id, | |
String? word, | |
String? meaning, | |
List<String>? synonyms = const [], | |
List<String>? examples = const [], | |
List<String>? mnemonics = const [], | |
DateTime? created_at}) { | |
return Word( | |
id ?? this.id, | |
word ?? this.word, | |
meaning ?? this.meaning, | |
examples: examples!.isEmpty ? this.examples : examples, | |
synonyms: synonyms!.isEmpty ? this.synonyms : synonyms, | |
mnemonics: mnemonics!.isEmpty ? this.mnemonics : mnemonics, | |
created_at: created_at ?? this.created_at, | |
); | |
} | |
@override | |
bool operator ==(Object other) => | |
identical(this, other) || | |
other is Word && | |
runtimeType == other.runtimeType && | |
id == other.id && | |
word == other.word && | |
meaning == other.meaning && | |
listEquals(synonyms, other.synonyms) && | |
listEquals(examples, other.examples) && | |
listEquals(mnemonics, other.mnemonics) && | |
created_at == other.created_at; | |
@override | |
int get hashCode => id.hashCode ^ word.hashCode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment