This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

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 Particular person s─▒n─▒f─▒n─▒ ele alacak olursak:

@freezed
class Particular person with _$Particular person {
  manufacturing unit Particular person(String title, int age) = _Person;
}
Enter fullscreen mode

Exit fullscreen mode

O zaman ┼čunu yazabiliriz:

var individual = Particular person('Remi', 24);

// `age` de─či┼čtirilmedi, de─čerini korur
print(individual.copyWith(title: 'Sprint')); // Particular person(title: Sprint, age: 24)
// `age` `null` olarak ayarland─▒
print(individual.copyWith(age: null)); // Particular person(title: 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 Firm with _$Firm {
  manufacturing unit Firm({String? title, Director? director}) = _Company;
}

@freezed
class Director with _$Director {
  manufacturing unit Director({String? title, Assistant? assistant}) = _Director;
}

@freezed
class Assistant with _$Assistant {
  manufacturing unit Assistant({String? title, int? age}) = _Assistant;
}
Enter fullscreen mode

Exit fullscreen mode

Ard─▒ndan, Firm ile ilgili bir referanstan Assistant ├╝zerinde de─či┼čiklikler yapmak isteyebiliriz.

├ľrne─čin, bir asistan─▒n title alan─▒n─▒ de─či┼čtirmek i├žin copyWith kullanarak ┼čunu yazmam─▒z gerekir:

Firm firm;

Firm newCompany = firm.copyWith(
  director: firm.director.copyWith(
    assistant: firm.director.assistant.copyWith(
      title: '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.

Firm firm;

Firm newCompany = firm.copyWith.director.assistant(title: 'John Smith');
Enter fullscreen mode

Exit fullscreen mode

Bu snippet, ├Ânceki snippet’le (g├╝ncellenmi┼č bir assistant title ile yeni bir firm olu┼čturma) kesinlikle ayn─▒ sonucu elde edecek, ancak art─▒k kopyalar─▒ yok.

Firm firm;
Firm newCompany = firm.copyWith.director(title: 'John Doe');
Enter fullscreen mode

Exit fullscreen mode

Genel olarak, yukar─▒da bahsedilen Firm/Director/Assistant tan─▒mlar─▒na g├Âre, a┼ča─č─▒daki t├╝m “copy” s├Âzdizimleri ├žal─▒┼čacakt─▒r:

Firm firm;

firm = firm.copyWith(title: 'Google', director: Director(...));
firm = firm.copyWith.director(title: 'Larry', assistant: Assistant(...));
firm = firm.copyWith.director.assistant(title: 'John', age: 42);
Enter fullscreen mode

Exit fullscreen mode

Bo┼č de─čerlendirme-Null consideration

Baz─▒ nesneler de bo┼č olabilir. ├ľrne─čin, Firm s─▒n─▒f─▒m─▒z─▒ kullanarak, Director null olabilir.

Buna g├Âre a┼ča─č─▒dakini yazmak mant─▒kl─▒ de─čil:

Firm firm = Firm(title: 'Google', director: null);
Firm newCompany = firm.copyWith.director.assistant(title: 'John');
Enter fullscreen mode

Exit fullscreen mode

Ba┼čta director yoksa director’├╝n asistan─▒n─▒ de─či┼čtiremeyiz.

Bu durumda, firm.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.

Firm? newCompany = firm.copyWith.director?.assistant(title: 'John');

Enter fullscreen mode

Exit fullscreen mode

Ayr─▒ca copyWith’i t├╝m constructor’larda tan─▒mlanan propertilerle kullanabilirsiniz:

var instance = Instance.individual('Remi', 24);
print(instance.copyWith(title: 'Sprint')); // Instance.individual(title: Sprint, age: 24)

instance = Instance.metropolis('London', 8900000);
print(instance.copyWith(title: 'Paris')); // Instance.metropolis(title: Paris, inhabitants: 8900000)
Enter fullscreen mode

Exit fullscreen mode

Reference

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?