Skip to content

Instantly share code, notes, and snippets.

@ResoDev

ResoDev/search_state.dart Secret

Created Jan 19, 2019
Embed
What would you like to do?
Custom factories for built values
//imports ...
abstract class SearchState implements Built<SearchState, SearchStateBuilder> {
bool get isLoading;
BuiltList<SearchItem> get searchResults;
String get error;
bool get hasReachedEndOfResults;
bool get isInitial =>
!isLoading && searchResults == BuiltList<SearchItem>() && error == '';
bool get isSuccessful =>
!isLoading && searchResults != BuiltList<SearchItem>() && error == '';
SearchState._();
factory SearchState([updates(SearchStateBuilder b)]) = _$SearchState;
factory SearchState.initial() {
return SearchState((b) => b
..isLoading = false
..searchResults.replace(BuiltList<SearchItem>())
..error = ''
..hasReachedEndOfResults = false);
}
factory SearchState.loading() {
return SearchState((b) => b
..isLoading = true
..searchResults.replace(BuiltList<SearchItem>())
..error = ''
..hasReachedEndOfResults = false);
}
factory SearchState.failure(String error) {
return SearchState((b) => b
..isLoading = false
..searchResults.replace(BuiltList<SearchItem>())
..error = error
..hasReachedEndOfResults = false);
}
factory SearchState.success(BuiltList<SearchItem> resultList) {
return SearchState((b) => b
..isLoading = false
..searchResults.replace(resultList)
..error = ''
..hasReachedEndOfResults = false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.