'How to refresh snapshot in flutter

I what to refresh home page so I create widget called RefreshWidget look like this

    import 'dart:io';        
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    class RefreshWidget extends StatefulWidget {
      // final GlobalKey<RefreshIndicatorState> keyRefresh;
      final Widget child;
      final Future Function() onRefresh;
    
      const RefreshWidget({
        Key? key,
        // required this.keyRefresh,
        required this.child,
        required this.onRefresh
      }) : super(key: key);
    
      @override
      _RefreshWidgetState createState() => _RefreshWidgetState();
    }
    
    class _RefreshWidgetState extends State<RefreshWidget> {
      @override
      Widget build(BuildContext context) =>
          Platform.isAndroid ? buildAndroidList() : buildIOSList();
    
      Widget buildAndroidList() => RefreshIndicator(
        // key: widget.keyRefresh,
        onRefresh: widget.onRefresh,
        child: widget.child,
      );
    
      Widget buildIOSList() => CustomScrollView(
        physics: BouncingScrollPhysics(),
        slivers: [
          CupertinoSliverRefreshControl(onRefresh: widget.onRefresh),
          SliverToBoxAdapter(child: widget.child),
        ],
      );
    }

I use it in home and it work just fine like this in side body after check the loading

RefreshWidget(
    onRefresh:loadPost,
    child: StreamBuilder(
        stream: homePosts,
        builder: (context,
            AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {

          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }

          int len = snapshot.data?.docs.length??0;
          for(int i = 0 ; i < len ; i++){
            _isloaded.add(false);
            userData.add('');
          }
          for(int i = 0 ; i < len ; i++){
            if(!_isloaded[i]) {
              getData(snapshot.data!.docs[i].data()['uid'], i);
            }
          }

          if (snapshot.data == null) {
            return Center(
              child: Container(
                child: Text(
                  "No posts yet!",
                  style: TextStyle(
                    color: Palette.textColor,
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            );
          }
         return PageView.builder( ...);

loadPost

      Future loadPost() async{
        getTheData();
        setState(() {
          homePosts = FirebaseFirestore.instance.collection('posts').orderBy("datePublished", descending: true).where('uid', whereIn: theUserData['following']).snapshots();
        });
      }

getTheData

/* get data method */
  getTheData() async {
    try {
      if ( uid!= null) {
        var userSnap = await FirebaseFirestore.instance
            .collection('users')
            .doc(uid)
            .get();

        /*end*/
        if (userSnap.data() != null) {
          theUserData = userSnap.data()!;
          theUserData['following'].add(uid);
          setState(() {
            _isTheUserLoaded = true;
            homePosts = FirebaseFirestore.instance.collection('posts').orderBy("datePublished", descending: true).where('uid', whereIn: theUserData['following']).snapshots();
          });

        } else
          Navigator.of(context).popAndPushNamed('/Signup_Login');
      }
    } catch (e) {
      showSnackBar(context, e.toString());
    }
  }

The problem

The post save uid. And I retrieve in the following code (copied from the first code I showed):

          int len = snapshot.data?.docs.length??0;
          for(int i = 0 ; i < len ; i++){
            _isloaded.add(false);
            userData.add('');
          }
          for(int i = 0 ; i < len ; i++){
            if(!_isloaded[i]) {
              getData(snapshot.data!.docs[i].data()['uid'], i);
            }
          }

When I unfollow someone userData that is a list of user info that posted post that will show in home, is not updated. There for the Home page will show the post with wrong user.

You can find the fill code here on Github: https://github.com/ShathaAldosari01/gp1_7_2022/blob/master/lib/screen/home/TimeLine/home_page.dart



Sources

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

Source: Stack Overflow

Solution Source