mirror of
https://github.com/wgh136/pixes.git
synced 2025-09-27 12:57:24 +00:00
Initial commit
This commit is contained in:
39
lib/network/res.dart
Normal file
39
lib/network/res.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
@immutable
|
||||
class Res<T>{
|
||||
///error info
|
||||
final String? errorMessage;
|
||||
|
||||
String get errorMessageWithoutNull => errorMessage??"Unknown Error";
|
||||
|
||||
/// data
|
||||
final T? _data;
|
||||
|
||||
/// is there an error
|
||||
bool get error => errorMessage!=null || _data==null;
|
||||
|
||||
/// whether succeed
|
||||
bool get success => !error;
|
||||
|
||||
/// data
|
||||
///
|
||||
/// must be called when no error happened, or it will throw error
|
||||
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.errorMessageWithoutNull;
|
||||
|
||||
/// network result
|
||||
const Res(this._data,{this.errorMessage, this.subData});
|
||||
|
||||
Res.error(dynamic e):errorMessage=e.toString(), _data=null, subData=null;
|
||||
}
|
Reference in New Issue
Block a user