Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save isaacadariku/305a90322209bbf53db426cd98779b31 to your computer and use it in GitHub Desktop.
Save isaacadariku/305a90322209bbf53db426cd98779b31 to your computer and use it in GitHub Desktop.
A sample of calling several endpoints with Stacked BaseViewModel
const String _endpoint1BusyKey = 'endpoint1Busy';
const String _endpoint2BusyKey = 'endpoint2Busy';
const String _endpoint3BusyKey = 'endpoint3Busy';
const String _endpoint4BusyKey = 'endpoint4Busy';
const String _endpoint5BusyKey = 'endpoint5Busy';
class EndpointViewmodel extends BaseViewModel {
final _endpointService = EndpointService();
// Values of the busy state of the endpoints.
bool get isEndpoint1Busy => busy(_endpoint1BusyKey);
bool get isEndpoint2Busy => busy(_endpoint2BusyKey);
bool get isEndpoint3Busy => busy(_endpoint3BusyKey);
bool get isEndpoint4Busy => busy(_endpoint4BusyKey);
bool get isEndpoint5Busy => busy(_endpoint5BusyKey);
// Values of the error state of the endpoints
// You can use a map to store the error state of the endpoints.
bool _errorOnEndpoint1 = false;
bool get errorOnEndpoint1 => _errorOnEndpoint1;
bool _errorOnEndpoint2 = false;
bool get errorOnEndpoint2 => _errorOnEndpoint2;
bool _errorOnEndpoint3 = false;
bool get errorOnEndpoint3 => _errorOnEndpoint3;
bool _errorOnEndpoint4 = false;
bool get errorOnEndpoint4 => _errorOnEndpoint4;
bool _errorOnEndpoint5 = false;
bool get errorOnEndpoint5 => _errorOnEndpoint5;
// Call this with onModelReady
Future<void> init() async {
// Running all endpoints in parallel
// Remove if any of them depend on the other endpoints.
await Future.wait([
_fetchEndpoint1(),
_fetchEndpoint2(),
_fetchEndpoint3(),
_fetchEndpoint4(),
_fetchEndpoint5(),
]);
}
Future<void> fetchEndpoint1() async {
try {
_errorOnEndpoint1 = false;
await runBusyFuture(
_endpointService.fetchEndpoint1(),
busyObject: _endpoint1BusyKey,
throwException: true,
);
} catch (err, stackTrace) {
log.e(err, stackTrace);
_errorOnEndpoint1 = true;
notifyListeners();
rethrow;
}
}
Future<void> fetchEndpoint2() async {
try {
_errorOnEndpoint2 = false;
await runBusyFuture(
_endpointService.fetchEndpoint2(),
busyObject: _endpoint2BusyKey,
throwException: true,
);
} catch (err, stackTrace) {
log.e(err, stackTrace);
_errorOnEndpoint2 = true;
notifyListeners();
rethrow;
}
}
Future<void> fetchEndpoint3() async {
try {
_errorOnEndpoint3 = false;
await runBusyFuture(
_endpointService.fetchEndpoint3(),
busyObject: _endpoint3BusyKey,
throwException: true,
);
} catch (err, stackTrace) {
log.e(err, stackTrace);
_errorOnEndpoint3 = true;
notifyListeners();
rethrow;
}
}
Future<void> fetchEndpoint4() async {
try {
_errorOnEndpoint4 = false;
await runBusyFuture(
_endpointService.fetchEndpoint4(),
busyObject: _endpoint4BusyKey,
throwException: true,
);
} catch (err, stackTrace) {
log.e(err, stackTrace);
_errorOnEndpoint4 = true;
notifyListeners();
rethrow;
}
}
Future<void> fetchEndpoint5() async {
try {
_errorOnEndpoint5 = false;
await runBusyFuture(
_endpointService.fetchEndpoint5(),
busyObject: _endpoint5BusyKey,
throwException: true,
);
} catch (err, stackTrace) {
log.e(err, stackTrace);
_errorOnEndpoint5 = true;
notifyListeners();
rethrow;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment