'I am gettign a "Dispose Error" on Login..not sure why though

I am getting the following error after logging out of my app and then logging back in.

>════════ Exception caught by widgets library ═══════════════════════════════════
A Post was used after being disposed.
The relevant error-causing widget was
PostItem
lib/widgets/posts_list.dart:18
════════════════════════════════════════════════════════════════════════════════

If I then do a hot restart the error goes away and all my posts are shown. Here is the code that is generating the error.

return ListView.builder(
      padding: const EdgeInsets.all(10.0),
      itemCount: posts.length,
      itemBuilder: (ctx, i) => ChangeNotifierProvider(
        create: (context) => posts[i],
        child: PostItem(),
      ),
    );

My main.dart file looks like this

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        //ALLOW MULTIPLE PROVIDERS
        providers: [
          ChangeNotifierProvider.value(
            value: Auth(),
          ),
          ChangeNotifierProxyProvider<Auth, Posts>(
            //NEED TO UNDERSTAND THIS
            update: (context, auth, previousPosts) => Posts(
              auth.token,
              previousPosts == null ? [] : previousPosts.items,
              auth.userId,
              auth.userName,
              auth.userImageUrl,
            ),
          ),
          
        ],
        child: Consumer<Auth>(
          builder: (context, auth, _) => MaterialApp(

While debugging and stepping through the code I see previousPosts.items is not being reset on logout. I think that may be the issue. How do I reset that? Or is it something else?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source