Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Freezed copyWith Method
Gülsen Keskin
Gülsen Keskin

Posted on • Edited on

     

Freezed copyWith Method

copyWith

copyWith methodu farklı değerlere sahip nesneleri klonlamayı sağlar.

Not: Birçok alternatifin aksine, Freezed kullanılırken copyWith yöntemi bir değere null değer atamayı doğru şekilde destekler.

Örnek olarak bir Person sınıfını ele alacak olursak:

@freezedclass Person with _$Person {  factory Person(String name, int age) = _Person;}
Enter fullscreen modeExit fullscreen mode

O zaman şunu yazabiliriz:

var person = Person('Remi', 24);// `age` değiştirilmedi, değerini korurprint(person.copyWith(name: 'Dash')); // Person(name: Dash, age: 24)// `age` `null` olarak ayarlandıprint(person.copyWith(age: null)); // Person(name: Remi, age: null)
Enter fullscreen modeExit fullscreen mode

CopyWith'in null parametreleri nasıl doğru bir şekilde anlayabildiğine dikkat edin.

Deep copy

Aşağıdaki sınıfları göz önünde bulunduracak olursak:

@freezedclass Company with _$Company {  factory Company({String? name, Director? director}) = _Company;}@freezedclass Director with _$Director {  factory Director({String? name, Assistant? assistant}) = _Director;}@freezedclass Assistant with _$Assistant {  factory Assistant({String? name, int? age}) = _Assistant;}
Enter fullscreen modeExit fullscreen mode

Ardından, Company ile ilgili bir referanstan Assistant üzerinde değişiklikler yapmak isteyebiliriz.

Örneğin, bir asistanın name alanını değiştirmek için copyWith kullanarak şunu yazmamız gerekir:

Company company;Company newCompany = company.copyWith(  director: company.director.copyWith(    assistant: company.director.assistant.copyWith(      name: 'John Smith',    ),  ),);
Enter fullscreen modeExit fullscreen mode

Bu işe yarar, ancak birçok kopya ile nispeten ayrıntılıdır. Burada Freezed'in "deep copy"sini kullanabiliriz.

Company company;Company newCompany = company.copyWith.director.assistant(name: 'John Smith');
Enter fullscreen modeExit fullscreen mode

Bu snippet, önceki snippet'le (güncellenmiş bir assistant name ile yeni bir company oluşturma) kesinlikle aynı sonucu elde edecek, ancak artık kopyaları yok.

Company company;Company newCompany = company.copyWith.director(name: 'John Doe');
Enter fullscreen modeExit fullscreen mode

Genel olarak, yukarıda bahsedilen Company/Director/Assistant tanımlarına göre, aşağıdaki tüm "copy" sözdizimleri çalışacaktır:

Company company;company = company.copyWith(name: 'Google', director: Director(...));company = company.copyWith.director(name: 'Larry', assistant: Assistant(...));company = company.copyWith.director.assistant(name: 'John', age: 42);
Enter fullscreen modeExit fullscreen mode

Boş değerlendirme-Null consideration

Bazı nesneler de boş olabilir. Örneğin, Company sınıfımızı kullanarak, Director null olabilir.

Buna göre aşağıdakini yazmak mantıklı değil:

Company company = Company(name: 'Google', director: null);Company newCompany = company.copyWith.director.assistant(name: 'John');
Enter fullscreen modeExit fullscreen mode

Başta director yoksa director'ün asistanını değiştiremeyiz.\

Bu durumda, company.copyWith.director null değerini döndürür ve önceki örneğimiz null istisnasıyla sonuçlanır.

Düzeltmek için ?. operatör'ünü kullanabiliriz.

Company? newCompany = company.copyWith.director?.assistant(name: 'John');
Enter fullscreen modeExit fullscreen mode

Ayrıca copyWith'i tüm constructor'larda tanımlanan propertilerle kullanabilirsiniz:

var example = Example.person('Remi', 24);print(example.copyWith(name: 'Dash')); // Example.person(name: Dash, age: 24)example = Example.city('London', 8900000);print(example.copyWith(name: 'Paris')); // Example.city(name: Paris, population: 8900000)
Enter fullscreen modeExit fullscreen mode

Reference

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

soft. dev. at harmonycloud
  • Location
    Sakarya, Türkiye
  • Education
    Pamukkale University
  • Work
    harmonyerp
  • Joined

More fromGülsen Keskin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp