Skip to content

Commit

Permalink
docs(flutter_infinite_list): update snippets to reflect latest code (#…
Browse files Browse the repository at this point in the history
…4318)

Co-authored-by: Felix Angelov <[email protected]>
  • Loading branch information
MinSeungHyun and felangel authored Jan 12, 2025
1 parent dddbbc4 commit cff02a3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@ import { Code } from '@astrojs/starlight/components';
const code = `
PostBloc({required this.httpClient}) : super(const PostState()) {
on<PostFetched>(_onPostFetched);
on<PostFetched>(_onFetched);
}
Future<void> _onPostFetched(PostFetched event, Emitter<PostState> emit) async {
Future<void> _onFetched(PostFetched event, Emitter<PostState> emit) async {
if (state.hasReachedMax) return;
try {
if (state.status == PostStatus.initial) {
final posts = await _fetchPosts();
return emit(state.copyWith(
status: PostStatus.success,
posts: posts,
hasReachedMax: false,
));
final posts = await _fetchPosts(startIndex: state.posts.length);
if (posts.isEmpty) {
return emit(state.copyWith(hasReachedMax: true));
}
final posts = await _fetchPosts(state.posts.length);
emit(posts.isEmpty
? state.copyWith(hasReachedMax: true)
: state.copyWith(
status: PostStatus.success,
posts: List.of(state.posts)..addAll(posts),
hasReachedMax: false,
));
emit(
state.copyWith(
status: PostStatus.success,
posts: [...state.posts, ...posts],
),
);
} catch (_) {
emit(state.copyWith(status: PostStatus.failure));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ EventTransformer<E> throttleDroppable<E>(Duration duration) {
class PostBloc extends Bloc<PostEvent, PostState> {
PostBloc({required this.httpClient}) : super(const PostState()) {
on<PostFetched>(
_onPostFetched,
_onFetched,
transformer: throttleDroppable(throttleDuration),
);
}
Expand Down
6 changes: 1 addition & 5 deletions docs/src/content/docs/tutorials/flutter-infinite-list.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ If we cannot retrieve the posts, we emit `PostStatus.failure`.

If we can retrieve the posts, we emit `PostStatus.success` and the entire list of posts.

One optimization we can make is to `throttle` the `PostFetched` event in order to prevent spamming our API unnecessarily. We can do this by using the `transform` parameter when we register the `_onPostFetched` event handler.
One optimization we can make is to `throttle` the `PostFetched` event in order to prevent spamming our API unnecessarily. We can do this by using the `transform` parameter when we register the `_onFetched` event handler.

:::note
Passing a `transformer` to `on<PostFetched>` allows us to customize how events are processed.
Expand Down Expand Up @@ -204,10 +204,6 @@ In our `main.dart` we can start by implementing our main function and calling `r
title="lib/main.dart"
/>

:::note
`EquatableConfig.stringify = kDebugMode;` is a constant that affects the output of toString. When in debug mode, equatable's toString method will behave differently than profile and release mode and can use constants like kDebugMode or kReleaseMode to understand if you are running on debug or release.
:::

In our `App` widget, the root of our project, we can then set the home to `PostsPage`

<RemoteCode
Expand Down

0 comments on commit cff02a3

Please sign in to comment.