Skip to content
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

Pagination approach at pub example won't dispose fetchPackagesProvider of a page even if it's out of screen #3602

Open
AhmedLSayed9 opened this issue Jun 7, 2024 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation needs triage

Comments

@AhmedLSayed9
Copy link
Contributor

AhmedLSayed9 commented Jun 7, 2024

The pagination approach at pub example (Also mentioned by Remi at FlutterVikings before) won't dispose fetchPackagesProvider of a page even if it's out of screen.

i.e: If you scroll down and load second page then scroll to the top, all PackageItem of the second page will be disposed but the corresponding provider fetchPackagesProvider of that page won't be disposed.

The reason is that we're using ref of the main SearchPage widget to fetch a page:

final packageList = ref.watch(

A potential solution is to wrap PackageItem with Consumer but that's not possible with declarative pagination as we need to return null to end the pagination.

Here's a minimal sample that clarifies the issue:

@riverpod
String test1(Test1Ref ref, int index) {
  ref.onDispose(() {
    print('dispose test1 at $index');
  });
  return index.toString();
}

@riverpod
String test2(Test2Ref ref, int index) {
  ref.onDispose(() {
    print('dispose test2 at $index');
  });
  return index.toString();
}

void main() {
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends ConsumerWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return MaterialApp(
      home: Scaffold(
        body: CustomScrollView(
          cacheExtent: 0,
          slivers: [
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (context, index) {
                  ref.watch(test1Provider(index));
                  return Consumer(
                    builder: (context, ref, child) {
                      ref.watch(test2Provider(index));
                      return ListTile(title: Text(index.toString()));
                    },
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
@AhmedLSayed9 AhmedLSayed9 added documentation Improvements or additions to documentation needs triage labels Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation needs triage
Projects
None yet
Development

No branches or pull requests

2 participants