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

The provider is destroyed when autodispose is not used #2844

Closed
wilson-orz opened this issue Aug 25, 2023 · 6 comments
Closed

The provider is destroyed when autodispose is not used #2844

wilson-orz opened this issue Aug 25, 2023 · 6 comments
Assignees
Labels
bug Something isn't working question Further information is requested
Milestone

Comments

@wilson-orz
Copy link

My application uses the StateNotifierProvider to save login user information and watch it somewhere, but the notifier sometimes gets destroyed when the application sleeps for a long time

@wilson-orz wilson-orz added bug Something isn't working needs triage labels Aug 25, 2023
@wilson-orz
Copy link
Author

final mainTabIndexProvider = Provider((ref) => PageController());

class MainTab extends ConsumerStatefulWidget {
  const MainTab({super.key});

  @override
  ConsumerState<MainTab> createState() => _MainTabState();
}

class _MainTabState extends ConsumerState<MainTab> {

  final barItemData = [
    {
      "name": "首页",
      "icon": "assets/svg/module/main/icon_main.svg",
      "selectedIcon": "assets/svg/module/main/icon_main_s.svg"
    },
    {
      "name": "发布",
      "icon": "assets/svg/module/main/icon_plus.svg",
      "selectedIcon": "assets/svg/module/main/icon_plus_s.svg"
    },
    {
      "name": "商城",
      "icon": "assets/svg/module/main/icon_shop.svg",
      "selectedIcon": "assets/svg/module/main/icon_shop_s.svg"
    },
    {
      "name": "我的",
      "icon": "assets/svg/module/main/icon_mine.svg",
      "selectedIcon": "assets/svg/module/main/icon_mine_s.svg"
    }
  ];

  late final pages = [const HomePage(), const PublishPage(), ShopWebPage(), const PersonalHomepage()];

  late final tabIndexProvider = StateProvider((ref) => 0);

  @override
  void initState() {
    super.initState();
    ref.read(mainTabIndexProvider).addListener(() {
      if (ref.read(mainTabIndexProvider).hasClients) {
        MyApp.refRead(tabIndexProvider.notifier).state = ref.read(mainTabIndexProvider).page!.round();
      }
    });
  }

  @override
  void dispose() {
    AppNotificationCenter.defaultCenter().removerObserver(target: this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Consumer(
        builder: (_, WidgetRef ref, __) {
          final index = ref.watch(tabIndexProvider);
          final hasCount = ref.watch(userProfileProvider.select((value) => value.hasCount));
          return BottomNavigationBar(
            onTap: (idx) {
              if (barItemData[idx]["name"] == "发布" || barItemData[idx]["name"] == "我的") {
                if (!MyApp.refRead(userProfileProvider.notifier).checkIsLogin(isExitLogin: true)) {
                  return;
                }
              }
              ref.read(mainTabIndexProvider).jumpToPage(idx);
            },
            currentIndex: index,
            items: [
              for (int i = 0; i < barItemData.length; i++)
                BottomNavigationBarItem(
                    activeIcon: _buildBottomIcon(barItemData[i]["selectedIcon"]!,
                        hasDot: i == barItemData.length - 1 && hasCount),
                    icon: _buildBottomIcon(barItemData[i]["icon"]!, hasDot: i == barItemData.length - 1 && hasCount),
                    label: barItemData[i]["name"]!),
            ],
            selectedLabelStyle: const TextStyle(fontSize: 10, fontWeight: FontWeight.bold, color: Colors.black),
            unselectedLabelStyle: const TextStyle(fontSize: 10, fontWeight: FontWeight.bold, color: Colors.black),
            showUnselectedLabels: true,
            showSelectedLabels: true,
            selectedItemColor: Colors.black,
            unselectedItemColor: const Color(0xFFCDCDCD),
            type: BottomNavigationBarType.fixed,
          );
        },
      ),
      body:  Consumer(
          builder: (_, WidgetRef ref, __) {
            return PageView(
              controller: ref.read(mainTabIndexProvider),
              physics: const NeverScrollableScrollPhysics(),
              children: pages,
            );
          },
        ),
    );
  }

}

@wilson-orz
Copy link
Author

When I use ref.read(mainTabIndexProvider), I can get the PageController, but wait a long time. ref.read(mainTabIndexProvider) gets a destroy error

@rrousselGit
Copy link
Owner

What's a "destroy error"?

Could you share a minimal reproducible sample?

If the application sleeps for a long time, it might've been killed by the OS itself. In which case this isn't about Riverpod

@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels Aug 28, 2023
@wilson-orz
Copy link
Author

sorry my poor english,i means i gets an error that the notifier was disposed,It happens mostly on Android.

@rrousselGit
Copy link
Owner

What error? :)

@rrousselGit rrousselGit added this to the Riverpod 3.0 milestone Oct 15, 2023
@rrousselGit
Copy link
Owner

In the lack of a reproducible example and clear description of the issue, I'm unable to do anything here. So I'll close this.

Please reopen a new issue if you still have the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants