63 lines
1.6 KiB
Dart
Executable File
63 lines
1.6 KiB
Dart
Executable File
import 'package:flutter/material.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
class RefWatchHandled<T> extends HookConsumerWidget {
|
|
const RefWatchHandled(
|
|
this.provider, {
|
|
super.key,
|
|
required this.onData,
|
|
this.onError,
|
|
this.demoErrorView = false,
|
|
this.demoLoadingView = false,
|
|
});
|
|
final ProviderListenable<AsyncValue<T>> provider;
|
|
final Widget Function(T data) onData;
|
|
final Widget Function(
|
|
Object error,
|
|
StackTrace stackTrace,
|
|
)? onError;
|
|
final bool demoErrorView;
|
|
final bool demoLoadingView;
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final AsyncValue<T> asyncVal = demoErrorView
|
|
? const AsyncError('err', StackTrace.empty)
|
|
: demoLoadingView
|
|
? const AsyncLoading()
|
|
: ref.watch(provider);
|
|
return asyncVal.when(
|
|
data: onData,
|
|
skipLoadingOnRefresh: false,
|
|
skipLoadingOnReload: false,
|
|
skipError: false,
|
|
error: (error, stackTrace) {
|
|
Future(
|
|
() => Error.throwWithStackTrace(
|
|
error,
|
|
stackTrace,
|
|
),
|
|
);
|
|
if (provider is ProviderBase) {
|
|
Future.delayed(
|
|
const Duration(seconds: 10),
|
|
() {
|
|
if (!context.mounted) return;
|
|
ref.invalidate(provider as ProviderBase);
|
|
},
|
|
);
|
|
}
|
|
if (onError != null) return onError!(error, stackTrace);
|
|
return const Center(
|
|
child: Text(
|
|
'Error. Trying again in 10 seconds',
|
|
),
|
|
);
|
|
},
|
|
loading: () => const Center(
|
|
child: CircularProgressIndicator(),
|
|
),
|
|
);
|
|
}
|
|
}
|