mirror of
https://github.com/venera-app/venera.git
synced 2025-09-27 07:47:24 +00:00
initial commit
This commit is contained in:
36
lib/foundation/res.dart
Normal file
36
lib/foundation/res.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
class Res<T> {
|
||||
/// error info
|
||||
final String? errorMessage;
|
||||
|
||||
/// data
|
||||
final T? _data;
|
||||
|
||||
/// is there an error
|
||||
bool get error => errorMessage != null;
|
||||
|
||||
/// whether succeed
|
||||
bool get success => !error;
|
||||
|
||||
/// data
|
||||
T get data => _data ?? (throw Exception(errorMessage));
|
||||
|
||||
/// get data, or null if there is an error
|
||||
T? get dataOrNull => _data;
|
||||
|
||||
final dynamic subData;
|
||||
|
||||
@override
|
||||
String toString() => _data.toString();
|
||||
|
||||
Res.fromErrorRes(Res another, {this.subData})
|
||||
: _data = null,
|
||||
errorMessage = another.errorMessage;
|
||||
|
||||
/// network result
|
||||
const Res(this._data, {this.errorMessage, this.subData});
|
||||
|
||||
const Res.error(String err)
|
||||
: _data = null,
|
||||
subData = null,
|
||||
errorMessage = err;
|
||||
}
|
Reference in New Issue
Block a user