import 'dart:async'; import '../base/base_response.dart'; import '../data/consts/error_code.dart'; import '../data/repository/account_repository.dart'; class HttpHandler { HttpHandler._(); static FutureOr Function(BaseResponse value) handle( bool allowEmptyData) { return (BaseResponse response) { if (response.code == 0) { if (response.data != null || allowEmptyData) { return response.data == null ? Future.value() : response.data!; } else { throw Exception('data is null'); } } else { if (response.code == ErrorCode.noLoginError) { AccountRepository.getInstance().logout(); } throw ServerErrorException(response.code, response.message); } }; } } class ServerErrorException implements Exception { final int? code; final String? message; ServerErrorException(this.code, this.message); @override String toString() { return 'ServerErrorException: code: $code, message: $message'; } }