refactored HTTP error handling and streamlined UI components by centralizing shared logic and removing redundant parameters
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
abstract class DataLoader<TResult> {
|
||||
final Dio dio;
|
||||
DataLoader(this.dio) {
|
||||
dio.options.connectTimeout = const Duration(seconds: 10);
|
||||
dio.options.sendTimeout = const Duration(seconds: 30);
|
||||
dio.options.receiveTimeout = const Duration(seconds: 30);
|
||||
}
|
||||
|
||||
Future<TResult> run() async {
|
||||
final response = await fetch();
|
||||
try {
|
||||
return assemble(
|
||||
DataLoaderResult(
|
||||
json: jsonDecode(response.data!),
|
||||
headers: response.headers.map.map(
|
||||
(key, value) => MapEntry(key, value.join(';')),
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (e, stack) {
|
||||
log('DataLoader assemble failed', error: e, stackTrace: stack);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Response<String>> fetch();
|
||||
TResult assemble(DataLoaderResult data);
|
||||
}
|
||||
|
||||
class DataLoaderResult {
|
||||
final dynamic json;
|
||||
final Map<String, String> headers;
|
||||
|
||||
Map<String, dynamic> asMap() => json as Map<String, dynamic>;
|
||||
List<dynamic> asList() => json as List<dynamic>;
|
||||
List<Map<String, dynamic>> asListOfMaps() =>
|
||||
asList().map((e) => e as Map<String, dynamic>).toList();
|
||||
|
||||
DataLoaderResult({required this.json, required this.headers});
|
||||
}
|
||||
@@ -19,12 +19,5 @@ abstract class LoadableState<TState> with _$LoadableState<TState> {
|
||||
String? statusText,
|
||||
}) = _LoadableState<TState>;
|
||||
|
||||
bool _hasError() => error != null;
|
||||
bool _hasData() => data != null;
|
||||
|
||||
bool showPrimaryLoading() => isLoading && !_hasData();
|
||||
bool showBackgroundLoading() => isLoading && _hasData();
|
||||
bool showErrorBar() => _hasError() && _hasData();
|
||||
bool showError() => _hasError() && !_hasData();
|
||||
bool showContent() => _hasData();
|
||||
bool showContent() => data != null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user