Skip to content

Instantly share code, notes, and snippets.

@matanshukry
Created July 3, 2022 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matanshukry/f19e334ae389233bf892753adb893e01 to your computer and use it in GitHub Desktop.
Save matanshukry/f19e334ae389233bf892753adb893e01 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
//*********************************************
// APP entry point
//*********************************************
void main() => runApp(
const ProviderScope(child: App()),
);
//*********************************************
// PROVIDERS
//*********************************************
// I couldn't import Dio here, so I'm just creating this "dump" dio class.
// but hte idea is to use it where I would need it in my actual app for http requests
class Dio {
final String baseUrl;
const Dio(this.baseUrl);
// fake, only used since we don't have url nor user
Future<User> get(String path) {
return Future.delayed(const Duration(seconds: 3), () => const User("fake-id-231"));
}
}
final tokenizedDio = FutureProvider.autoDispose<Dio>((ref) async {
// mimic the idea of a url coming from a remote config.
const duration = Duration(seconds: 5);
final url = await Future.delayed(duration, () => 'http://google.com/api');
return Dio(url);
});
final userApiProvider = Provider<UserApi>((ref) {
// I want to get a Future<Dio>, so that waiting on it would result in some Dio
// that the UserApiImpl can use
// final Future<Dio> futureDio = ref.watch(tokenizedDio);
final Future<Dio> futureDio = ref.read(tokenizedDio.future);
return UserApiImpl(futureDio);
});
//*********************************************
// API
//*********************************************
class User {
final String id;
const User(this.id);
}
abstract class UserApi {
Future<User> getCurrentUser();
}
class UserApiImpl implements UserApi {
final Future<Dio> _dio;
const UserApiImpl(this._dio);
static const kPathCurrentUser = "/api/user/@me";
@override
Future<User> getCurrentUser() async {
final dio = await _dio;
final response = await dio.get(kPathCurrentUser);
// this
return response;
}
}
//*********************************************
// App root
//*********************************************
class App extends ConsumerWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) => const MaterialApp(
title: 'Riverpod Navigator Example',
debugShowCheckedModeBanner: false,
home: HomeScreen()
);
}
//*********************************************
// Widgets
//*********************************************
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Sample.1'),
),
body: const Center(
child: WaitForUserWidget()
),
);
}
class WaitForUserWidget extends ConsumerWidget {
const WaitForUserWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
final userApi = ref.watch(userApiProvider);
return FutureBuilder<User>(future: userApi.getCurrentUser(), builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
final User user = snapshot.data!;
return Text(user.id);
}
return const CircularProgressIndicator();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment