Last updated:
0 purchases
flutter async hook
flutter_async_hook #
Flutter hook for future async operations.
Features #
✅ useAsyncSnapshot - Hook for async operations.
Getting started #
In the pubspec.yaml of your flutter project, add the following dependency:
dependencies:
...
flutter_async_hook: any
copied to clipboard
Usage #
import 'package:flutter_async_hook/flutter_async_hook.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
void main() {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends HookWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
final state = useAsyncSnapshot<String>();
return Column(
children: [
if (state.isLoading) const LinearProgressIndicator(),
state.value.when(
idle: () {
return const Text('Idle');
},
data: (data) {
return Text('Data: $data');
},
error: (error, stackTrace) {
return Text('Error: $error');
},
loading: () {
return const CircularProgressIndicator();
},
),
FilledButton(
onPressed: () async {
// TODO(you): write loading state here
await state(_greeting());
state.whenDataOrError(
data: (data) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Data: $data')),
);
},
error: (error, stackTrace) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: $error')),
);
},
);
},
child: const Text('Run async'),
),
],
);
}
Future<String> _greeting() {
return Future.delayed(const Duration(seconds: 2), () => 'Hello');
}
}
copied to clipboard
Additional information #
This package is based on the flutter_hooks package.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.