Skip to content

Commit

Permalink
feat(example): add minimal example for cacheDataFor
Browse files Browse the repository at this point in the history
  • Loading branch information
giboin committed Jul 4, 2024
1 parent aac527f commit eae6a16
Show file tree
Hide file tree
Showing 7 changed files with 534 additions and 7 deletions.
63 changes: 63 additions & 0 deletions example/lib/home_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:example/providers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class HomeScreen extends ConsumerStatefulWidget {
const HomeScreen({
super.key,
});

@override
ConsumerState<ConsumerStatefulWidget> createState() {
return _HomeScreenState();
}
}

class _HomeScreenState extends ConsumerState<HomeScreen> {
@override
Widget build(BuildContext context) {
Future<int>? data = ref.read(cacheDataForProvider.future);

return Scaffold(
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Gets mock data. After 3 seconds, it returns 42, and then cache the result for 4 seconds.',
),
const Divider(indent: 20, endIndent: 20),
Row(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
child: const Text(
'get data once again',
),
onPressed: () {
setState(() {
data = ref.read(cacheDataForProvider.future);
});
},
),
const SizedBox(width: 10),
Padding(
padding: const EdgeInsets.all(8.0),
child: FutureBuilder(
future: data,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Text('data:${snapshot.data.toString()}');
}
return const CircularProgressIndicator();
}),
),
],
)
],
),
),
);
}
}
10 changes: 5 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:example/home_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
runApp(const MainApp());
Expand All @@ -9,11 +11,9 @@ class MainApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: Center(
child: Text('Hello World!'),
),
return const ProviderScope(
child: MaterialApp(
home: HomeScreen(),
),
);
}
Expand Down
11 changes: 11 additions & 0 deletions example/lib/providers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:riverpod_community_extensions/riverpod_community_extensions.dart';

part 'providers.g.dart';

@riverpod
Future<int> cacheDataFor(CacheDataForRef ref) async {
ref.cacheDataFor(const Duration(seconds: 4));
await Future.delayed(const Duration(seconds: 3));
return 42;
}
24 changes: 24 additions & 0 deletions example/lib/providers.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eae6a16

Please sign in to comment.