Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:math';
- import 'package:dio/dio.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- void main() {
- runApp(
- ProviderScope(child: const MainApp()),
- );
- }
- class MainApp extends StatelessWidget {
- const MainApp({super.key});
- @override
- Widget build(BuildContext context) {
- return const MaterialApp(
- home: PostScreen(),
- );
- }
- }
- final baseUrlProvider = StateProvider<String>((ref) {
- return 'https://mybackend.com';
- });
- final perPageProvider = StateProvider<int>((ref) {
- return 1;
- });
- final dioProvider = Provider.family<Dio, String>((ref, url) {
- final dio = Dio(
- BaseOptions(
- baseUrl: url,
- ),
- );
- return dio;
- });
- final postRepoProvider =
- Provider.autoDispose.family<PostRepository, String>((ref, baseUrl) {
- final dio = ref.watch(dioProvider(baseUrl));
- return PostRepository(dio: dio);
- });
- class PostRepository {
- PostRepository({required this.dio});
- final Dio dio;
- final Random random = Random();
- Future<List<String>> fetchPosts({required int perPage}) async {
- await Future.delayed(const Duration(milliseconds: 500));
- return List.generate(
- perPage,
- (index) =>
- 'Post $perPage - ${random.nextInt(perPage)}, ${dio.options.baseUrl}');
- }
- }
- final postsProvider = AsyncNotifierProvider.autoDispose
- .family<PostNotifier, List<String>, String>(PostNotifier.new);
- class PostNotifier
- extends AutoDisposeFamilyAsyncNotifier<List<String>, String> {
- @override
- FutureOr<List<String>> build(String arg) {
- final perPage = ref.watch(perPageProvider);
- return ref.watch(postRepoProvider(arg)).fetchPosts(
- perPage: perPage,
- );
- }
- }
- class PostScreen extends ConsumerWidget {
- const PostScreen({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- return const Scaffold(
- body: SafeArea(
- child: Column(
- children: [
- Row(
- children: [
- UpdateUrlButton(),
- UpdatePerPageButton(),
- ],
- ),
- PerPageText(),
- UrlDisplayText(),
- Expanded(child: PostList()),
- ],
- ),
- ),
- );
- }
- }
- class UpdateUrlButton extends ConsumerWidget {
- const UpdateUrlButton({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final rnd = Random();
- return FilledButton(
- onPressed: () {
- ref.read(baseUrlProvider.notifier).state =
- 'https://example.com/${rnd.nextInt(100)}';
- },
- child: const Text('Update URL'),
- );
- }
- }
- class UpdatePerPageButton extends ConsumerWidget {
- const UpdatePerPageButton({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final perPage = ref.watch(perPageProvider);
- return FilledButton(
- onPressed: () {
- ref.read(perPageProvider.notifier).state = perPage + 1;
- },
- child: const Text('Update Per Page'),
- );
- }
- }
- class PostList extends ConsumerWidget {
- const PostList({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final baseUrl = ref.watch(baseUrlProvider);
- return ref.watch(postsProvider(baseUrl)).when(
- data: (posts) {
- return ListView.builder(
- itemCount: posts.length,
- itemBuilder: (context, index) {
- final data = posts[index].split(', ');
- return ListTile(
- title: Text(data[0]),
- subtitle: Text(data[1]),
- );
- },
- );
- },
- loading: () => const Center(
- child: CircularProgressIndicator(),
- ),
- error: (error, stackTrace) => Center(
- child: Text('Error: $error'),
- ),
- );
- }
- }
- class PerPageText extends ConsumerWidget {
- const PerPageText({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final count = ref.watch(perPageProvider);
- return Text('Per page: $count');
- }
- }
- class UrlDisplayText extends ConsumerWidget {
- const UrlDisplayText({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final url = ref.watch(baseUrlProvider);
- return Text('URL: $url');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement