I'm getting this error on a starting example that I'm writing
type 'Error<dynamic>' is not a subtype of type 'FutureOr<Success<List<TaskEntity>>>'
I've created a generic Result class like this:
enum DataStatus { local, remote, error }
enum ErrorStatus { unknown, backend_error, timeout }
@sealed
abstract class Result<T> {}
class Success<T> extends Result<T> {
final T data;
final DataStatus dataStatus;
final String message;
Success({this.data, this.dataStatus, this.message});
}
class Error extends Result {
final Exception exception;
final ErrorStatus errorStatus;
final String message;
Error({this.exception, this.errorStatus, this.message});
}
On my repository I have this method:
Future<Result<List<TaskEntity>>> getTasks() {
return remoteDataSource
.getTasks()
.then((value) => Success(
data: value.map((e) => e.toTaskEntity()).toList(),
dataStatus: DataStatus.remote))
.catchError((Object obj) {
switch (obj.runtimeType) {
case DioError:
final dioErrorResponse = (obj as DioError).response;
return Error(
exception: Exception(dioErrorResponse.statusMessage),
errorStatus: ErrorStatus.backend_error,
message: dioErrorResponse.statusMessage);
break;
default:
}
});
My approach is to use this class to return the responses and If there's an error return the error.
Right now I'm blocked because I'm getting this error
type 'Error<dynamic>' is not a subtype of type 'FutureOr<Success<List<TaskEntity>>>'
I also tried to use the freezed library but no luck.
part 'result.freezed.dart';
@freezed
abstract class Result<T> with _$Result<T> {
const factory Result.success({T data, DataStatus dataStatus, String message}) = Success<T>;
const factory Result.error({Exception exception, ErrorStatus errorStatus, String message}) = Error;
}
question from:
https://stackoverflow.com/questions/65904772/dart-problem-replicating-sealed-classes-like-kotlin 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…