-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example): add minimal example for cacheDataFor
- Loading branch information
Showing
7 changed files
with
534 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}), | ||
), | ||
], | ||
) | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.