mirror of
https://github.com/flutter/samples.git
synced 2025-11-10 14:58:34 +00:00
Compass app (#2446)
This commit is contained in:
54
compass_app/app/lib/utils/result.dart
Normal file
54
compass_app/app/lib/utils/result.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright 2024 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
/// Utility class to wrap result data
|
||||
///
|
||||
/// Evaluate the result using a switch statement:
|
||||
/// ```dart
|
||||
/// switch (result) {
|
||||
/// case Ok(): {
|
||||
/// print(result.value);
|
||||
/// }
|
||||
/// case Error(): {
|
||||
/// print(result.error);
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
sealed class Result<T> {
|
||||
const Result();
|
||||
|
||||
/// Creates an instance of Result containing a value
|
||||
factory Result.ok(T value) => Ok(value);
|
||||
|
||||
/// Create an instance of Result containing an error
|
||||
factory Result.error(Exception error) => Error(error);
|
||||
|
||||
/// Convenience method to cast to Ok
|
||||
Ok<T> get asOk => this as Ok<T>;
|
||||
|
||||
/// Convenience method to cast to Error
|
||||
Error get asError => this as Error<T>;
|
||||
}
|
||||
|
||||
/// Subclass of Result for values
|
||||
final class Ok<T> extends Result<T> {
|
||||
const Ok(this.value);
|
||||
|
||||
/// Returned value in result
|
||||
final T value;
|
||||
|
||||
@override
|
||||
String toString() => 'Result<$T>.ok($value)';
|
||||
}
|
||||
|
||||
/// Subclass of Result for errors
|
||||
final class Error<T> extends Result<T> {
|
||||
const Error(this.error);
|
||||
|
||||
/// Returned error in result
|
||||
final Exception error;
|
||||
|
||||
@override
|
||||
String toString() => 'Result<$T>.error($error)';
|
||||
}
|
||||
Reference in New Issue
Block a user