flutter-hydrated-bloc-tutorial's People
flutter-hydrated-bloc-tutorial's Issues
HydratedBloc the last state does not return
BenefitsState my code
@immutable
abstract class BenefitsState extends Equatable {}
class BenefitsLoading extends BenefitsState {
@override
List<Object> get props => [];
}
class BenefitsLoad extends BenefitsState {
final List<BenefitModel> benefits;
BenefitsLoad({this.benefits});
static BenefitsLoad get initialState => BenefitsLoad(
benefits: [],
);
BenefitsLoad copyWith({
List<BenefitModel> benefits,
}) {
return BenefitsLoad(
benefits: benefits ?? this.benefits,
);
}
@override
List<Object> get props => [benefits];
factory BenefitsLoad.fromMap(Map<String, dynamic> map) => BenefitsLoad(
benefits: map['benefits'] == null ? null : List<BenefitModel>.from(map['benefits']?.map((x) => BenefitModel.fromJson(x))),
);
Map<String, dynamic> toMap() {
return {
'benefits': benefits == null ? null : List<dynamic>.from(benefits.map((x) => x.toJson())),
};
}
}
My bloc
class BenefitsBloc extends HydratedBloc<BenefitsEvent, BenefitsState> {
GetBenefitsAllUsecase getBenefitsAllUsecase;
BenefitsBloc({
@required GetBenefitsAllUsecase getBenefitsAllUsecase,
}) : this.getBenefitsAllUsecase = getBenefitsAllUsecase,
super(BenefitsLoad.initialState ?? BenefitsLoad());
@override
Stream<BenefitsState> mapEventToState(
BenefitsEvent event,
) async* {
if (event is GetBenefist) {
try {
print(state);
yield BenefitsLoading();
var response = await getBenefitsAllUsecase();
var data = ApiResponse.completed(response);
yield BenefitsLoad(benefits: data.data);
} catch (e) {
yield state;
}
}
}
@override
BenefitsState fromJson(Map<String, dynamic> json) {
print('fromJson');
try {
final weather = BenefitsLoad.fromMap(json);
return BenefitsLoad(benefits: weather.benefits);
} catch (_) {
return null;
}
}
@override
Map<String, dynamic> toJson(BenefitsState state) {
print("toJson");
if (state is BenefitsLoad) {
return state.toMap();
} else {
return null;
}
}
}
What's my problem:
My application is mostly without internet.
So when it loads the first time the BenefitsLoad state is cached with an array of data.
but when it does not detect that there is no internet, then it jumps to the try catch, and I want to return the previous state, but unfortunately it returns the BenefistLoading
Am I doing something wrong?
JSON Conversion
Serializing to JSON is very easy with our previous setup. We just have to make sure the state being serialized is BenefitsLoad, as the other ones cannot be serialized. Otherwise, we will return null.Returning null will leave the stored state as it is. This means that when the user exits the app before a new weather could be loaded, he will be presented with the previous weather upon launching the app.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.