-
-
Notifications
You must be signed in to change notification settings - Fork 974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add more examples and enhance docs with tutorials #3932
Comments
I'd like to get on that, if it's up for grabs 😀 From what I'm reading in the issue description, this could be achieved with:
Which tbh I would split into two PR's I guess 🤔 |
Current examples are meh. We need to make new examples, where every one of them demonstrates one specific thing. One Firebase integration would be cool. Maybe one for go_router. It sounds commonly requested. Although you'll need my review for that. And maybe one for shared_prefs or sqlite. Then an example with a rest API ... All those could be both in the form of "example" and "tutorial". |
Don't do everything in one PR. |
Thanks for the feedback and interest, everyone! @BenAuerDev, since there are multiple tutorials and examples to create, we can split the work. For instance, I’d be happy to work on the Firebase login example and a corresponding tutorial. What do you think about focusing on the REST API example or go_router? We can align patterns together to maintain consistency. Let me know your thoughts on how we can coordinate - I'm excited to collaborate! |
Hey @thisissandipp I'd love to collaborate on this 😃 💪 thanks Amazing 🎉 I'll start with REST API then. After that we still need examples for:
maye also And what about the basic examples like |
I'd delete them.
What would be the added value over the rest API example? go_router has the added value of showcasing how to convert providers to ValueListenables |
ok :) but don't you think some beginner friendly tutorials might be nice?
Sorry I meant So I'll start with rest API 🙌
I might have some more questions about that once I'll start doing go_router :) |
Hey guys, I found an example for handling REST Api that is short but (imho) demonstrates it quite well. But I'm keen to hear what you think :) Example:import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() {
runApp(ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: UserListScreen(),
);
}
}
// Model
class User {
final int id;
final String name;
final String email;
User({required this.id, required this.name, required this.email});
factory User.fromJson(Map<String, dynamic> json) => User(
id: json['id'],
name: json['name'],
email: json['email'],
);
}
// API Repository
class UserRepository {
final Dio dio;
UserRepository(this.dio);
Future<List<User>> fetchUsers() async {
try {
final response =
await dio.get('https://jsonplaceholder.typicode.com/users');
return (response.data as List)
.map((userData) => User.fromJson(userData))
.toList();
} catch (e) {
throw Exception('Failed to load users');
}
}
}
// Providers
final dioProvider = Provider((ref) => Dio());
final userRepositoryProvider = Provider((ref) {
return UserRepository(ref.watch(dioProvider));
});
final usersProvider = AsyncNotifierProvider<UsersNotifier, List<User>>(() {
return UsersNotifier();
});
// Notifier for Users State
class UsersNotifier extends AsyncNotifier<List<User>> {
@override
Future<List<User>> build() async {
final repository = ref.read(userRepositoryProvider);
return repository.fetchUsers();
}
Future<void> refreshUsers() async {
state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
return ref.read(userRepositoryProvider).fetchUsers();
});
}
}
// User List Screen
class UserListScreen extends ConsumerWidget {
const UserListScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final usersAsync = ref.watch(usersProvider);
return Scaffold(
appBar: AppBar(title: Text('Users')),
body: usersAsync.when(
data: (users) => RefreshIndicator(
onRefresh: () => ref.read(usersProvider.notifier).refreshUsers(),
child: ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) {
final user = users[index];
return ListTile(
title: Text(user.name),
subtitle: Text(user.email),
);
},
),
),
error: (error, stack) => Center(child: Text('Error: $error')),
loading: () => Center(child: CircularProgressIndicator()),
),
);
}
} Furthermore I was wondering how to organize the
Let me know if I forgot something :) that you think we should also take into account or something :) |
@rrousselGit |
No that I understood :) I just wondered if we should simply have:
or
Noted I'll use an example that uses both Thanks for the feedback 💪 🙌 |
Describe what scenario you think is uncovered by the existing examples/articles
Describe why existing examples/articles do not cover this case
The text was updated successfully, but these errors were encountered: