/
easy_example_provider.dart
39 lines (34 loc) · 1.32 KB
/
easy_example_provider.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_infinite_scroll/riverpod_infinite_scroll.dart';
class Post {
final int id;
final String title;
final String image;
const Post({required this.id, required this.title, required this.image});
}
class EasyExampleNotifier extends PagedNotifier<int, Post> {
EasyExampleNotifier()
: super(
load: (page, limit) =>
Future.delayed(const Duration(milliseconds: 500), () {
// This simulates a network call to an api that returns paginated posts
return List.generate(
20,
(index) => Post(
id: index,
title: "My ${index + ((limit * page) - limit)} work",
image: "https://via.placeholder.com/150/92c952"));
}),
nextPageKeyBuilder: NextPageKeyBuilderDefault.mysqlPagination,
);
// Super simple example of custom methods of the StateNotifier
void add(Post post) {
state = state.copyWith(records: [...(state.records ?? []), post]);
}
void delete(Post post) {
state = state.copyWith(records: [...(state.records ?? [])]..remove(post));
}
}
final easyExampleProvider =
StateNotifierProvider<EasyExampleNotifier, PagedState<int, Post>>(
(_) => EasyExampleNotifier());