mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
refactor Result class, remove asOk and asError (#2542)
As discussed in the PR for the Result pattern implementation (https://github.com/flutter/website/pull/11444) @parlough recommended that `asError` and `asOk` should be not be used, and instead we should use proper exhaustiveness checking. This PR removes the two "convenience" methods and refactors code. In some cases, it was enough with writing a proper `if` clause, while in others it was necessary to use a `switch`. Still, they are present in the `testing` folder, as they can be useful for testing purposes. ## Pre-launch Checklist - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I have added sample code updates to the [changelog]. - [x] I updated/added relevant documentation (doc comments with `///`). If you need help, consider asking for advice on the #hackers-devrel channel on [Discord]. <!-- Links --> [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [Contributors Guide]: https://github.com/flutter/samples/blob/main/CONTRIBUTING.md [changelog]: ../CHANGELOG.md
This commit is contained in:
@@ -8,6 +8,7 @@ import 'package:logging/logging.dart';
|
||||
import '../../../data/repositories/activity/activity_repository.dart';
|
||||
import '../../../data/repositories/itinerary_config/itinerary_config_repository.dart';
|
||||
import '../../../domain/models/activity/activity.dart';
|
||||
import '../../../domain/models/itinerary_config/itinerary_config.dart';
|
||||
import '../../../utils/command.dart';
|
||||
import '../../../utils/result.dart';
|
||||
|
||||
@@ -45,21 +46,23 @@ class ActivitiesViewModel extends ChangeNotifier {
|
||||
|
||||
Future<Result<void>> _loadActivities() async {
|
||||
final result = await _itineraryConfigRepository.getItineraryConfig();
|
||||
if (result is Error) {
|
||||
_log.warning(
|
||||
'Failed to load stored ItineraryConfig',
|
||||
result.asError.error,
|
||||
);
|
||||
return result;
|
||||
switch (result) {
|
||||
case Error<ItineraryConfig>():
|
||||
_log.warning(
|
||||
'Failed to load stored ItineraryConfig',
|
||||
result.error,
|
||||
);
|
||||
return result;
|
||||
case Ok<ItineraryConfig>():
|
||||
}
|
||||
|
||||
final destinationRef = result.asOk.value.destination;
|
||||
final destinationRef = result.value.destination;
|
||||
if (destinationRef == null) {
|
||||
_log.severe('Destination missing in ItineraryConfig');
|
||||
return Result.error(Exception('Destination not found'));
|
||||
}
|
||||
|
||||
_selectedActivities.addAll(result.asOk.value.activities);
|
||||
_selectedActivities.addAll(result.value.activities);
|
||||
|
||||
final resultActivities =
|
||||
await _activityRepository.getByDestination(destinationRef);
|
||||
@@ -120,21 +123,23 @@ class ActivitiesViewModel extends ChangeNotifier {
|
||||
|
||||
Future<Result<void>> _saveActivities() async {
|
||||
final resultConfig = await _itineraryConfigRepository.getItineraryConfig();
|
||||
if (resultConfig is Error) {
|
||||
_log.warning(
|
||||
'Failed to load stored ItineraryConfig',
|
||||
resultConfig.asError.error,
|
||||
);
|
||||
return resultConfig;
|
||||
switch (resultConfig) {
|
||||
case Error<ItineraryConfig>():
|
||||
_log.warning(
|
||||
'Failed to load stored ItineraryConfig',
|
||||
resultConfig.error,
|
||||
);
|
||||
return resultConfig;
|
||||
case Ok<ItineraryConfig>():
|
||||
}
|
||||
|
||||
final itineraryConfig = resultConfig.asOk.value;
|
||||
final itineraryConfig = resultConfig.value;
|
||||
final result = await _itineraryConfigRepository.setItineraryConfig(
|
||||
itineraryConfig.copyWith(activities: _selectedActivities.toList()));
|
||||
if (result is Error) {
|
||||
_log.warning(
|
||||
'Failed to store ItineraryConfig',
|
||||
result.asError.error,
|
||||
result.error,
|
||||
);
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -65,7 +65,7 @@ class BookingViewModel extends ChangeNotifier {
|
||||
case Error<Booking>():
|
||||
_log.warning('Booking error: ${result.error}');
|
||||
notifyListeners();
|
||||
return Result.error(result.asError.error);
|
||||
return Result.error(result.error);
|
||||
}
|
||||
case Error<ItineraryConfig>():
|
||||
_log.warning('ItineraryConfig error: ${itineraryConfig.error}');
|
||||
|
||||
@@ -50,14 +50,16 @@ class ResultsViewModel extends ChangeNotifier {
|
||||
Future<Result<void>> _search() async {
|
||||
// Load current itinerary config
|
||||
final resultConfig = await _itineraryConfigRepository.getItineraryConfig();
|
||||
if (resultConfig is Error) {
|
||||
_log.warning(
|
||||
'Failed to load stored ItineraryConfig',
|
||||
resultConfig.asError.error,
|
||||
);
|
||||
return resultConfig;
|
||||
switch (resultConfig) {
|
||||
case Error<ItineraryConfig>():
|
||||
_log.warning(
|
||||
'Failed to load stored ItineraryConfig',
|
||||
resultConfig.error,
|
||||
);
|
||||
return resultConfig;
|
||||
case Ok<ItineraryConfig>():
|
||||
}
|
||||
_itineraryConfig = resultConfig.asOk.value;
|
||||
_itineraryConfig = resultConfig.value;
|
||||
notifyListeners();
|
||||
|
||||
final result = await _destinationRepository.getDestinations();
|
||||
@@ -86,15 +88,17 @@ class ResultsViewModel extends ChangeNotifier {
|
||||
assert(destinationRef.isNotEmpty, "destinationRef should not be empty");
|
||||
|
||||
final resultConfig = await _itineraryConfigRepository.getItineraryConfig();
|
||||
if (resultConfig is Error) {
|
||||
_log.warning(
|
||||
'Failed to load stored ItineraryConfig',
|
||||
resultConfig.asError.error,
|
||||
);
|
||||
return resultConfig;
|
||||
switch (resultConfig) {
|
||||
case Error<ItineraryConfig>():
|
||||
_log.warning(
|
||||
'Failed to load stored ItineraryConfig',
|
||||
resultConfig.error,
|
||||
);
|
||||
return resultConfig;
|
||||
case Ok<ItineraryConfig>():
|
||||
}
|
||||
|
||||
final itineraryConfig = resultConfig.asOk.value;
|
||||
final itineraryConfig = resultConfig.value;
|
||||
final result = await _itineraryConfigRepository
|
||||
.setItineraryConfig(itineraryConfig.copyWith(
|
||||
destination: destinationRef,
|
||||
@@ -103,7 +107,7 @@ class ResultsViewModel extends ChangeNotifier {
|
||||
if (result is Error) {
|
||||
_log.warning(
|
||||
'Failed to store ItineraryConfig',
|
||||
result.asError.error,
|
||||
result.error,
|
||||
);
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -99,14 +99,10 @@ class SearchFormViewModel extends ChangeNotifier {
|
||||
final result = await _continentRepository.getContinents();
|
||||
switch (result) {
|
||||
case Ok():
|
||||
{
|
||||
_continents = result.value;
|
||||
_log.fine('Continents (${_continents.length}) loaded');
|
||||
}
|
||||
_continents = result.value;
|
||||
_log.fine('Continents (${_continents.length}) loaded');
|
||||
case Error():
|
||||
{
|
||||
_log.warning('Failed to load continents', result.asError.error);
|
||||
}
|
||||
_log.warning('Failed to load continents', result.error);
|
||||
}
|
||||
notifyListeners();
|
||||
return result;
|
||||
@@ -116,27 +112,23 @@ class SearchFormViewModel extends ChangeNotifier {
|
||||
final result = await _itineraryConfigRepository.getItineraryConfig();
|
||||
switch (result) {
|
||||
case Ok<ItineraryConfig>():
|
||||
{
|
||||
final itineraryConfig = result.value;
|
||||
_selectedContinent = itineraryConfig.continent;
|
||||
if (itineraryConfig.startDate != null &&
|
||||
itineraryConfig.endDate != null) {
|
||||
_dateRange = DateTimeRange(
|
||||
start: itineraryConfig.startDate!,
|
||||
end: itineraryConfig.endDate!,
|
||||
);
|
||||
}
|
||||
_guests = itineraryConfig.guests ?? 0;
|
||||
_log.fine('ItineraryConfig loaded');
|
||||
notifyListeners();
|
||||
}
|
||||
case Error<ItineraryConfig>():
|
||||
{
|
||||
_log.warning(
|
||||
'Failed to load stored ItineraryConfig',
|
||||
result.asError.error,
|
||||
final itineraryConfig = result.value;
|
||||
_selectedContinent = itineraryConfig.continent;
|
||||
if (itineraryConfig.startDate != null &&
|
||||
itineraryConfig.endDate != null) {
|
||||
_dateRange = DateTimeRange(
|
||||
start: itineraryConfig.startDate!,
|
||||
end: itineraryConfig.endDate!,
|
||||
);
|
||||
}
|
||||
_guests = itineraryConfig.guests ?? 0;
|
||||
_log.fine('ItineraryConfig loaded');
|
||||
notifyListeners();
|
||||
case Error<ItineraryConfig>():
|
||||
_log.warning(
|
||||
'Failed to load stored ItineraryConfig',
|
||||
result.error,
|
||||
);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user