Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does copyWith() function do?

Tags:

flutter

dart

While learning flutter_bloc from bloclibrary website, I had to use a function with the constructor named copyWith(). Can anyone explain to me what exactly it does?

like image 610
Ambani saheb Avatar asked Sep 17 '25 18:09

Ambani saheb


2 Answers

Usually, copyWith() creates a clone of the object with provided parameters overridden.

like image 183
Styx Avatar answered Sep 19 '25 07:09

Styx


The copywith function creates a copy of poststate with some modification so that you don't need to create the whole state again.

The author said in his blog.

"We implemented copyWith so that we can copy an instance of PostState and update zero or more properties conveniently (this will come in handy later )".

For more info, you can read the following document.

https://github.com/felangel/bloc/blob/master/docs/cs/flutterinfinitelisttutorial.md

like image 33
Soham Pandya Avatar answered Sep 19 '25 08:09

Soham Pandya