mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
[compass_app] Standardize on Result factories rather than constructors (#2538)
From my review of the recipes PR in https://github.com/flutter/website/pull/11444#pullrequestreview-2480423811.
This commit is contained in:
@@ -16,12 +16,12 @@ class AuthRepositoryDev extends AuthRepository {
|
|||||||
required String email,
|
required String email,
|
||||||
required String password,
|
required String password,
|
||||||
}) async {
|
}) async {
|
||||||
return Result.ok(null);
|
return const Result.ok(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Logout is always successful in dev scenarios
|
/// Logout is always successful in dev scenarios
|
||||||
@override
|
@override
|
||||||
Future<Result<void>> logout() async {
|
Future<Result<void>> logout() async {
|
||||||
return Result.ok(null);
|
return const Result.ok(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class BookingRepositoryLocal implements BookingRepository {
|
|||||||
// Bookings created come without id, we need to assign one
|
// Bookings created come without id, we need to assign one
|
||||||
final bookingWithId = booking.copyWith(id: _sequentialId++);
|
final bookingWithId = booking.copyWith(id: _sequentialId++);
|
||||||
_bookings.add(bookingWithId);
|
_bookings.add(bookingWithId);
|
||||||
return Result.ok(null);
|
return const Result.ok(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -92,6 +92,6 @@ class BookingRepositoryLocal implements BookingRepository {
|
|||||||
@override
|
@override
|
||||||
Future<Result<void>> delete(int id) async {
|
Future<Result<void>> delete(int id) async {
|
||||||
_bookings.removeWhere((booking) => booking.id == id);
|
_bookings.removeWhere((booking) => booking.id == id);
|
||||||
return Result.ok(null);
|
return const Result.ok(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,6 @@ class ItineraryConfigRepositoryMemory implements ItineraryConfigRepository {
|
|||||||
ItineraryConfig itineraryConfig,
|
ItineraryConfig itineraryConfig,
|
||||||
) async {
|
) async {
|
||||||
_itineraryConfig = itineraryConfig;
|
_itineraryConfig = itineraryConfig;
|
||||||
return Result.ok(true);
|
return const Result.ok(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ class ApiClient {
|
|||||||
return Result.ok(
|
return Result.ok(
|
||||||
json.map((element) => Continent.fromJson(element)).toList());
|
json.map((element) => Continent.fromJson(element)).toList());
|
||||||
} else {
|
} else {
|
||||||
return Result.error(const HttpException("Invalid response"));
|
return const Result.error(HttpException("Invalid response"));
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
@@ -74,7 +74,7 @@ class ApiClient {
|
|||||||
return Result.ok(
|
return Result.ok(
|
||||||
json.map((element) => Destination.fromJson(element)).toList());
|
json.map((element) => Destination.fromJson(element)).toList());
|
||||||
} else {
|
} else {
|
||||||
return Result.error(const HttpException("Invalid response"));
|
return const Result.error(HttpException("Invalid response"));
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
@@ -97,7 +97,7 @@ class ApiClient {
|
|||||||
json.map((element) => Activity.fromJson(element)).toList();
|
json.map((element) => Activity.fromJson(element)).toList();
|
||||||
return Result.ok(activities);
|
return Result.ok(activities);
|
||||||
} else {
|
} else {
|
||||||
return Result.error(const HttpException("Invalid response"));
|
return const Result.error(HttpException("Invalid response"));
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
@@ -119,7 +119,7 @@ class ApiClient {
|
|||||||
json.map((element) => BookingApiModel.fromJson(element)).toList();
|
json.map((element) => BookingApiModel.fromJson(element)).toList();
|
||||||
return Result.ok(bookings);
|
return Result.ok(bookings);
|
||||||
} else {
|
} else {
|
||||||
return Result.error(const HttpException("Invalid response"));
|
return const Result.error(HttpException("Invalid response"));
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
@@ -139,7 +139,7 @@ class ApiClient {
|
|||||||
final booking = BookingApiModel.fromJson(jsonDecode(stringData));
|
final booking = BookingApiModel.fromJson(jsonDecode(stringData));
|
||||||
return Result.ok(booking);
|
return Result.ok(booking);
|
||||||
} else {
|
} else {
|
||||||
return Result.error(const HttpException("Invalid response"));
|
return const Result.error(HttpException("Invalid response"));
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
@@ -160,7 +160,7 @@ class ApiClient {
|
|||||||
final booking = BookingApiModel.fromJson(jsonDecode(stringData));
|
final booking = BookingApiModel.fromJson(jsonDecode(stringData));
|
||||||
return Result.ok(booking);
|
return Result.ok(booking);
|
||||||
} else {
|
} else {
|
||||||
return Result.error(const HttpException("Invalid response"));
|
return const Result.error(HttpException("Invalid response"));
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
@@ -180,7 +180,7 @@ class ApiClient {
|
|||||||
final user = UserApiModel.fromJson(jsonDecode(stringData));
|
final user = UserApiModel.fromJson(jsonDecode(stringData));
|
||||||
return Result.ok(user);
|
return Result.ok(user);
|
||||||
} else {
|
} else {
|
||||||
return Result.error(const HttpException("Invalid response"));
|
return const Result.error(HttpException("Invalid response"));
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
@@ -197,9 +197,9 @@ class ApiClient {
|
|||||||
final response = await request.close();
|
final response = await request.close();
|
||||||
// Response 204 "No Content", delete was successful
|
// Response 204 "No Content", delete was successful
|
||||||
if (response.statusCode == 204) {
|
if (response.statusCode == 204) {
|
||||||
return Result.ok(null);
|
return const Result.ok(null);
|
||||||
} else {
|
} else {
|
||||||
return Result.error(const HttpException("Invalid response"));
|
return const Result.error(HttpException("Invalid response"));
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class AuthApiClient {
|
|||||||
final stringData = await response.transform(utf8.decoder).join();
|
final stringData = await response.transform(utf8.decoder).join();
|
||||||
return Result.ok(LoginResponse.fromJson(jsonDecode(stringData)));
|
return Result.ok(LoginResponse.fromJson(jsonDecode(stringData)));
|
||||||
} else {
|
} else {
|
||||||
return Result.error(const HttpException("Login error"));
|
return const Result.error(HttpException("Login error"));
|
||||||
}
|
}
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class SharedPreferencesService {
|
|||||||
_log.finer('Replaced token');
|
_log.finer('Replaced token');
|
||||||
await sharedPreferences.setString(_tokenKey, token);
|
await sharedPreferences.setString(_tokenKey, token);
|
||||||
}
|
}
|
||||||
return Result.ok(null);
|
return const Result.ok(null);
|
||||||
} on Exception catch (e) {
|
} on Exception catch (e) {
|
||||||
_log.warning('Failed to set token', e);
|
_log.warning('Failed to set token', e);
|
||||||
return Result.error(e);
|
return Result.error(e);
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ class BookingCreateUseCase {
|
|||||||
case Ok<List<Destination>>():
|
case Ok<List<Destination>>():
|
||||||
final destination = result.value
|
final destination = result.value
|
||||||
.firstWhere((destination) => destination.ref == destinationRef);
|
.firstWhere((destination) => destination.ref == destinationRef);
|
||||||
return Ok(destination);
|
return Result.ok(destination);
|
||||||
case Error<List<Destination>>():
|
case Error<List<Destination>>():
|
||||||
return Result.error(result.error);
|
return Result.error(result.error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class BookingShareUseCase {
|
|||||||
try {
|
try {
|
||||||
await _share(text);
|
await _share(text);
|
||||||
_log.fine('Shared booking');
|
_log.fine('Shared booking');
|
||||||
return Result.ok(null);
|
return const Result.ok(null);
|
||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
_log.severe('Failed to share booking', error);
|
_log.severe('Failed to share booking', error);
|
||||||
return Result.error(error);
|
return Result.error(error);
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ class BookingViewModel extends ChangeNotifier {
|
|||||||
_log.fine('Created Booking');
|
_log.fine('Created Booking');
|
||||||
_booking = result.value;
|
_booking = result.value;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
return Result.ok(null);
|
return const Result.ok(null);
|
||||||
case Error<Booking>():
|
case Error<Booking>():
|
||||||
_log.warning('Booking error: ${result.error}');
|
_log.warning('Booking error: ${result.error}');
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|||||||
@@ -18,11 +18,11 @@
|
|||||||
sealed class Result<T> {
|
sealed class Result<T> {
|
||||||
const Result();
|
const Result();
|
||||||
|
|
||||||
/// Creates an instance of Result containing a value
|
/// Creates a successful [Result], completed with the specified [value].
|
||||||
factory Result.ok(T value) => Ok(value);
|
const factory Result.ok(T value) = Ok._;
|
||||||
|
|
||||||
/// Create an instance of Result containing an error
|
/// Creates an error [Result], completed with the specified [error].
|
||||||
factory Result.error(Exception error) => Error(error);
|
const factory Result.error(Exception error) = Error._;
|
||||||
|
|
||||||
/// Convenience method to cast to Ok
|
/// Convenience method to cast to Ok
|
||||||
Ok<T> get asOk => this as Ok<T>;
|
Ok<T> get asOk => this as Ok<T>;
|
||||||
@@ -33,7 +33,7 @@ sealed class Result<T> {
|
|||||||
|
|
||||||
/// Subclass of Result for values
|
/// Subclass of Result for values
|
||||||
final class Ok<T> extends Result<T> {
|
final class Ok<T> extends Result<T> {
|
||||||
const Ok(this.value);
|
const Ok._(this.value);
|
||||||
|
|
||||||
/// Returned value in result
|
/// Returned value in result
|
||||||
final T value;
|
final T value;
|
||||||
@@ -44,7 +44,7 @@ final class Ok<T> extends Result<T> {
|
|||||||
|
|
||||||
/// Subclass of Result for errors
|
/// Subclass of Result for errors
|
||||||
final class Error<T> extends Result<T> {
|
final class Error<T> extends Result<T> {
|
||||||
const Error(this.error);
|
const Error._(this.error);
|
||||||
|
|
||||||
/// Returned error in result
|
/// Returned error in result
|
||||||
final Exception error;
|
final Exception error;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class FakeAuthApiClient implements AuthApiClient {
|
|||||||
@override
|
@override
|
||||||
Future<Result<LoginResponse>> login(LoginRequest loginRequest) async {
|
Future<Result<LoginResponse>> login(LoginRequest loginRequest) async {
|
||||||
if (loginRequest.email == 'EMAIL' && loginRequest.password == 'PASSWORD') {
|
if (loginRequest.email == 'EMAIL' && loginRequest.password == 'PASSWORD') {
|
||||||
return Result.ok(const LoginResponse(token: 'TOKEN', userId: '123'));
|
return const Result.ok(LoginResponse(token: 'TOKEN', userId: '123'));
|
||||||
}
|
}
|
||||||
return Result.error(Exception('ERROR!'));
|
return Result.error(Exception('ERROR!'));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user