mirror of
https://github.com/hmalik144/days_left_flutter.git
synced 2025-12-10 03:05:21 +00:00
- Introduction on base view model and stateless widget classes
- Implementation of firebase - Implementation of dependency injection Took 11 hours 26 minutes
This commit is contained in:
41
lib/base/BaseModel.dart
Normal file
41
lib/base/BaseModel.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:days_left/data/ViewState.dart';
|
||||
import 'package:stacked/stacked.dart';
|
||||
|
||||
class BaseModel extends BaseViewModel {
|
||||
ViewState _viewState = Idle();
|
||||
|
||||
ViewState get viewState => _viewState;
|
||||
|
||||
void onStart() {
|
||||
_viewState = HasStarted();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void onSuccess(dynamic data) {
|
||||
_viewState = HasData(data);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void onError(String error) {
|
||||
_viewState = HasError(error);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void handleFuture(Future<dynamic> func) {
|
||||
onStart();
|
||||
func.then((value) => onSuccess(value)).catchError((error) {
|
||||
print(error);
|
||||
onError(error);
|
||||
});
|
||||
}
|
||||
|
||||
void handleStream(Stream<dynamic> stream) {
|
||||
stream.listen((event) {
|
||||
onSuccess(event);
|
||||
}).onError((handleError) {
|
||||
onError(handleError);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user