Skip to content

Commit c3c93a8

Browse files
authored
[compass_app] Standardize on Result factories rather than constructors (#2538)
From my review of the recipes PR in flutter/website#11444 (review).
1 parent 33701ce commit c3c93a8

File tree

11 files changed

+26
-26
lines changed

11 files changed

+26
-26
lines changed

compass_app/app/lib/data/repositories/auth/auth_repository_dev.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class AuthRepositoryDev extends AuthRepository {
1616
required String email,
1717
required String password,
1818
}) async {
19-
return Result.ok(null);
19+
return const Result.ok(null);
2020
}
2121

2222
/// Logout is always successful in dev scenarios
2323
@override
2424
Future<Result<void>> logout() async {
25-
return Result.ok(null);
25+
return const Result.ok(null);
2626
}
2727
}

compass_app/app/lib/data/repositories/booking/booking_repository_local.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class BookingRepositoryLocal implements BookingRepository {
3131
// Bookings created come without id, we need to assign one
3232
final bookingWithId = booking.copyWith(id: _sequentialId++);
3333
_bookings.add(bookingWithId);
34-
return Result.ok(null);
34+
return const Result.ok(null);
3535
}
3636

3737
@override
@@ -92,6 +92,6 @@ class BookingRepositoryLocal implements BookingRepository {
9292
@override
9393
Future<Result<void>> delete(int id) async {
9494
_bookings.removeWhere((booking) => booking.id == id);
95-
return Result.ok(null);
95+
return const Result.ok(null);
9696
}
9797
}

compass_app/app/lib/data/repositories/itinerary_config/itinerary_config_repository_memory.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ class ItineraryConfigRepositoryMemory implements ItineraryConfigRepository {
2222
ItineraryConfig itineraryConfig,
2323
) async {
2424
_itineraryConfig = itineraryConfig;
25-
return Result.ok(true);
25+
return const Result.ok(true);
2626
}
2727
}

compass_app/app/lib/data/services/api/api_client.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ApiClient {
5353
return Result.ok(
5454
json.map((element) => Continent.fromJson(element)).toList());
5555
} else {
56-
return Result.error(const HttpException("Invalid response"));
56+
return const Result.error(HttpException("Invalid response"));
5757
}
5858
} on Exception catch (error) {
5959
return Result.error(error);
@@ -74,7 +74,7 @@ class ApiClient {
7474
return Result.ok(
7575
json.map((element) => Destination.fromJson(element)).toList());
7676
} else {
77-
return Result.error(const HttpException("Invalid response"));
77+
return const Result.error(HttpException("Invalid response"));
7878
}
7979
} on Exception catch (error) {
8080
return Result.error(error);
@@ -97,7 +97,7 @@ class ApiClient {
9797
json.map((element) => Activity.fromJson(element)).toList();
9898
return Result.ok(activities);
9999
} else {
100-
return Result.error(const HttpException("Invalid response"));
100+
return const Result.error(HttpException("Invalid response"));
101101
}
102102
} on Exception catch (error) {
103103
return Result.error(error);
@@ -119,7 +119,7 @@ class ApiClient {
119119
json.map((element) => BookingApiModel.fromJson(element)).toList();
120120
return Result.ok(bookings);
121121
} else {
122-
return Result.error(const HttpException("Invalid response"));
122+
return const Result.error(HttpException("Invalid response"));
123123
}
124124
} on Exception catch (error) {
125125
return Result.error(error);
@@ -139,7 +139,7 @@ class ApiClient {
139139
final booking = BookingApiModel.fromJson(jsonDecode(stringData));
140140
return Result.ok(booking);
141141
} else {
142-
return Result.error(const HttpException("Invalid response"));
142+
return const Result.error(HttpException("Invalid response"));
143143
}
144144
} on Exception catch (error) {
145145
return Result.error(error);
@@ -160,7 +160,7 @@ class ApiClient {
160160
final booking = BookingApiModel.fromJson(jsonDecode(stringData));
161161
return Result.ok(booking);
162162
} else {
163-
return Result.error(const HttpException("Invalid response"));
163+
return const Result.error(HttpException("Invalid response"));
164164
}
165165
} on Exception catch (error) {
166166
return Result.error(error);
@@ -180,7 +180,7 @@ class ApiClient {
180180
final user = UserApiModel.fromJson(jsonDecode(stringData));
181181
return Result.ok(user);
182182
} else {
183-
return Result.error(const HttpException("Invalid response"));
183+
return const Result.error(HttpException("Invalid response"));
184184
}
185185
} on Exception catch (error) {
186186
return Result.error(error);
@@ -197,9 +197,9 @@ class ApiClient {
197197
final response = await request.close();
198198
// Response 204 "No Content", delete was successful
199199
if (response.statusCode == 204) {
200-
return Result.ok(null);
200+
return const Result.ok(null);
201201
} else {
202-
return Result.error(const HttpException("Invalid response"));
202+
return const Result.error(HttpException("Invalid response"));
203203
}
204204
} on Exception catch (error) {
205205
return Result.error(error);

compass_app/app/lib/data/services/api/auth_api_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AuthApiClient {
3232
final stringData = await response.transform(utf8.decoder).join();
3333
return Result.ok(LoginResponse.fromJson(jsonDecode(stringData)));
3434
} else {
35-
return Result.error(const HttpException("Login error"));
35+
return const Result.error(HttpException("Login error"));
3636
}
3737
} on Exception catch (error) {
3838
return Result.error(error);

compass_app/app/lib/data/services/shared_preferences_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SharedPreferencesService {
3232
_log.finer('Replaced token');
3333
await sharedPreferences.setString(_tokenKey, token);
3434
}
35-
return Result.ok(null);
35+
return const Result.ok(null);
3636
} on Exception catch (e) {
3737
_log.warning('Failed to set token', e);
3838
return Result.error(e);

compass_app/app/lib/domain/use_cases/booking/booking_create_use_case.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class BookingCreateUseCase {
9898
case Ok<List<Destination>>():
9999
final destination = result.value
100100
.firstWhere((destination) => destination.ref == destinationRef);
101-
return Ok(destination);
101+
return Result.ok(destination);
102102
case Error<List<Destination>>():
103103
return Result.error(result.error);
104104
}

compass_app/app/lib/domain/use_cases/booking/booking_share_use_case.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class BookingShareUseCase {
3737
try {
3838
await _share(text);
3939
_log.fine('Shared booking');
40-
return Result.ok(null);
40+
return const Result.ok(null);
4141
} on Exception catch (error) {
4242
_log.severe('Failed to share booking', error);
4343
return Result.error(error);

compass_app/app/lib/ui/booking/view_models/booking_viewmodel.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class BookingViewModel extends ChangeNotifier {
6161
_log.fine('Created Booking');
6262
_booking = result.value;
6363
notifyListeners();
64-
return Result.ok(null);
64+
return const Result.ok(null);
6565
case Error<Booking>():
6666
_log.warning('Booking error: ${result.error}');
6767
notifyListeners();

compass_app/app/lib/utils/result.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
sealed class Result<T> {
1919
const Result();
2020

21-
/// Creates an instance of Result containing a value
22-
factory Result.ok(T value) => Ok(value);
21+
/// Creates a successful [Result], completed with the specified [value].
22+
const factory Result.ok(T value) = Ok._;
2323

24-
/// Create an instance of Result containing an error
25-
factory Result.error(Exception error) => Error(error);
24+
/// Creates an error [Result], completed with the specified [error].
25+
const factory Result.error(Exception error) = Error._;
2626

2727
/// Convenience method to cast to Ok
2828
Ok<T> get asOk => this as Ok<T>;
@@ -33,7 +33,7 @@ sealed class Result<T> {
3333

3434
/// Subclass of Result for values
3535
final class Ok<T> extends Result<T> {
36-
const Ok(this.value);
36+
const Ok._(this.value);
3737

3838
/// Returned value in result
3939
final T value;
@@ -44,7 +44,7 @@ final class Ok<T> extends Result<T> {
4444

4545
/// Subclass of Result for errors
4646
final class Error<T> extends Result<T> {
47-
const Error(this.error);
47+
const Error._(this.error);
4848

4949
/// Returned error in result
5050
final Exception error;

0 commit comments

Comments
 (0)