Bloc: Bloc State

Created on 25 May 2020  路  1Comment  路  Source: felangel/bloc

Hi,
I am using single state concept in my project like (your below example). It is very easy and useful for me. But i want to know What is purpose, How to use and Where to use of "copyWith" function. Can you please give me some sample code for that.

Example code:

`
class NewCarState {
final List brands;
final String brand;

final List models;
final String model;

final List years;
final String year;

const NewCarState({
@required this.brands,
@required this.brand,
@required this.models,
@required this.model,
@required this.years,
@required this.year,
});

bool get isComplete => brand != null && model != null && year != null;

factory NewCarState.initial() => NewCarState(
brands: [],
brand: null,
models: [],
model: null,
years: [],
year: null,
);

factory NewCarState.brandsLoading() => NewCarState(
brands: [],
brand: null,
models: [],
model: null,
years: [],
year: null,
);

factory NewCarState.brandsLoaded({
@required List brands,
}) =>
NewCarState(
brands: brands,
brand: null,
models: [],
model: null,
years: [],
year: null,
);

factory NewCarState.modelsLoading({
@required List brands,
@required String brand,
}) =>
NewCarState(
brands: brands,
brand: brand,
models: [],
model: null,
years: [],
year: null,
);

factory NewCarState.modelsLoaded({
@required List brands,
@required String brand,
@required List models,
}) =>
NewCarState(
brands: brands,
brand: brand,
models: models,
model: null,
years: [],
year: null,
);

factory NewCarState.yearsLoading({
@required List brands,
@required String brand,
@required List models,
@required String model,
}) =>
NewCarState(
brands: brands,
brand: brand,
models: models,
model: model,
years: [],
year: null,
);

factory NewCarState.yearsLoaded({
@required List brands,
@required String brand,
@required List models,
@required String model,
@required List years,
}) =>
NewCarState(
brands: brands,
brand: brand,
models: models,
model: model,
years: years,
year: null,
);

NewCarState copyWith({
List brands,
String brand,
List models,
String model,
List years,
String year,
}) {
return NewCarState(
brands: brands ?? this.brands,
brand: brand ?? this.brand,
models: models ?? this.models,
model: model ?? this.model,
years: years ?? this.years,
year: year ?? this.year,
);
}
}`

Thanks,
Ganesh. S

question

Most helpful comment

Hi @GaneshSingaravelu 馃憢

copyWith is a pattern used when working with immutable objects; it allows to create a new instance while also preserving unspecified values from an existing instance.

yield state.copyWith(brand: 'brand new brand(couldn't help myself)');

This will create a new instance of NewCarState with your new specified brand AND will also copy all other existing values from the previous instance.

Hope it makes sense 馃憤

>All comments

Hi @GaneshSingaravelu 馃憢

copyWith is a pattern used when working with immutable objects; it allows to create a new instance while also preserving unspecified values from an existing instance.

yield state.copyWith(brand: 'brand new brand(couldn't help myself)');

This will create a new instance of NewCarState with your new specified brand AND will also copy all other existing values from the previous instance.

Hope it makes sense 馃憤

Was this page helpful?
0 / 5 - 0 ratings