DEV Community

Cover image for Flutter copyWith Method ­čĺź ­čîî ÔťĘ
G├╝lsen Keskin
G├╝lsen Keskin

Posted on

Flutter 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:

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

O zaman ┼čunu yazabiliriz:

var person = Person('Remi', 24);

// `age` de─či┼čtirilmedi, de─čerini korur
print(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 mode Exit 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:

@freezed
class Company with _$Company {
  factory Company({String? name, Director? director}) = _Company;
}

@freezed
class Director with _$Director {
  factory Director({String? name, Assistant? assistant}) = _Director;
}

@freezed
class Assistant with _$Assistant {
  factory Assistant({String? name, int? age}) = _Assistant;
}
Enter fullscreen mode Exit 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 mode Exit 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 mode Exit 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 mode Exit 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 mode Exit 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 mode Exit 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 mode Exit 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 mode Exit fullscreen mode

Reference

Discussion (0)