Last active
July 31, 2021 03:41
-
-
Save bizz84/e22d6091c2fc0df56c438760305bc177 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import 'package:flutter/material.dart'; | |
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
| void main() { | |
| runApp(ProviderScope(child: MyApp())); | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: AuthWidget(), | |
| ); | |
| } | |
| } | |
| class AuthWidget extends ConsumerWidget { | |
| const AuthWidget({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context, WidgetRef ref) { | |
| return Scaffold( | |
| body: Builder(builder: (context) { | |
| final authStateChanges = ref.watch(authStateChangesProvider); | |
| return authStateChanges.when( | |
| data: (uid) { | |
| if (uid != null) { | |
| return HomePage(); | |
| } else { | |
| return SignInPage(); | |
| } | |
| }, | |
| loading: () => Center(child: CircularProgressIndicator()), | |
| error: (e, st) => Text('authStateChanges error: $e'), | |
| ); | |
| }), | |
| ); | |
| } | |
| } | |
| class SignInPage extends StatelessWidget { | |
| const SignInPage({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| print('build SignInPage'); | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text('SignInPage'), | |
| ), | |
| body: Center(child: Text('SIGN IN')), | |
| ); | |
| } | |
| } | |
| class HomePage extends ConsumerWidget { | |
| const HomePage({Key? key}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context, WidgetRef ref) { | |
| print('build HomePage'); | |
| final jobsStream = ref.watch(someFutureProvider); | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text('HomePage'), | |
| ), | |
| body: Center(child: Builder(builder: (context) { | |
| return jobsStream.when( | |
| data: (data) => Text(data), | |
| loading: () => CircularProgressIndicator(), | |
| error: (e, st) => Text('Error: $e'), | |
| ); | |
| })), | |
| ); | |
| } | |
| } | |
| Stream<String?> authStateChanges() async* { | |
| for (var i = 0; i < 3; i++) { | |
| yield i % 2 == 0 ? i.toString() : null; | |
| await Future.delayed(Duration(seconds: 5)); | |
| } | |
| } | |
| class Database { | |
| Database(this.uid); | |
| final String uid; | |
| Future<String> someFuture() async { | |
| await Future.delayed(Duration(seconds: 2)); | |
| return '$uid-data'; | |
| } | |
| } | |
| final authStateChangesProvider = StreamProvider<String?>((ref) { | |
| return authStateChanges(); | |
| }); | |
| final databaseProvider = Provider<Database>((ref) { | |
| final auth = ref.watch(authStateChangesProvider); | |
| if (auth.data?.value != null) { | |
| print('return Database'); | |
| return Database(auth.data!.value!); | |
| } | |
| print('throw UnimplementedError'); | |
| throw UnimplementedError(); | |
| }); | |
| final someFutureProvider = FutureProvider.autoDispose<String>((ref) { | |
| final database = ref.watch(databaseProvider); | |
| print('rebuild someFutureProvider'); | |
| return database.someFuture(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment