From fa8f2a1f35f98eed7da75fa45327856b6f4c58a4 Mon Sep 17 00:00:00 2001 From: Piyush Date: Mon, 3 Jun 2024 23:36:38 +0530 Subject: [PATCH] fix the same bug in infinite query observer --- lib/src/infinite_query_observer.dart | 41 +++++++++++++++------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/src/infinite_query_observer.dart b/lib/src/infinite_query_observer.dart index 3a8cf94..80c334c 100644 --- a/lib/src/infinite_query_observer.dart +++ b/lib/src/infinite_query_observer.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; import 'package:fquery/fquery.dart'; import 'package:fquery/src/query.dart'; import 'package:fquery/src/retry_resolver.dart'; @@ -81,26 +82,28 @@ class InfiniteQueryObserver extends ChangeNotifier final isInvalidated = query.state.isInvalidated; // [RefetchOnMount] behavior is specified here - if (isRefetching && !isInvalidated) { - switch (options.refetchOnMount) { - case RefetchOnMount.always: - refetch(); - break; - case RefetchOnMount.stale: - DateTime? staleAt = - query.state.dataUpdatedAt?.add(options.staleDuration); - final isStale = staleAt?.isBefore(DateTime.now()) ?? true; - if (isStale) refetch(); - break; - case RefetchOnMount.never: - break; + WidgetsBinding.instance.addPostFrameCallback((_) { + if (isRefetching && !isInvalidated) { + switch (options.refetchOnMount) { + case RefetchOnMount.always: + refetch(); + break; + case RefetchOnMount.stale: + DateTime? staleAt = + query.state.dataUpdatedAt?.add(options.staleDuration); + final isStale = staleAt?.isBefore(DateTime.now()) ?? true; + if (isStale) refetch(); + break; + case RefetchOnMount.never: + break; + } + } else { + fetch( + options.initialPageParam, + FetchMeta(direction: FetchDirection.forward), + ); } - } else { - fetch( - options.initialPageParam, - FetchMeta(direction: FetchDirection.forward), - ); - } + }); } /// Takes a [UseInfiniteQueryOptions] and sets the [options] field.