Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I tried two posibilities but the firebase data isnt being stored in the products list.The app page returns no data as there is nothing in procucts list. foollowing list returns data succesfully.The second possibilty that i tried and the Consolelog is in this pastebin link below: https://pastebin.com/DM47YHY6

         List<String> foollowingList = [];
List<NetworkImage> _listOfImages = <NetworkImage>[];
List<DocumentSnapshot> products = []; // stores fetched products
bool isLoading = true; // track if products fetching
bool hasMore = true; // flag for more products available or not
int documentLimit = 10; // documents to be fetched per request
DocumentSnapshot lastDocument; // flag for last document from where next 10 records to be fetched
ScrollController _scrollController = ScrollController(); /
void initState() {
  super.initState();
  getFfollowing();
  getPosts();
  _scrollController.addListener(() {
    double maxScroll = _scrollController.position.maxScrollExtent;
    double currentScroll = _scrollController.position.pixels;
    double delta = MediaQuery.of(context).size.height * 0.20;
    if (maxScroll - currentScroll <= delta) {
      getMorePosts();
    }
  });

}
getFfollowing() async {
  QuerySnapshot snapshot = await followingRef
      .document(currentUser.id)
      .collection('userFollowing')
      .getDocuments();
  setState(() {
    foollowingList = snapshot.documents.map((doc) => doc.documentID).toList();
  });
}

getPosts()async{

  for( int i=0; i< foollowingList.length; i++) {
    Query q  =  Firestore.instance
        .collection('posts/${foollowingList[i]}/userPosts')
        .orderBy('ownerId')
        .orderBy('timestamp', descending: true)
        .limit(documentLimit);
    setState(() {
      isLoading = true;
    });
    QuerySnapshot querySnapshot = await q.getDocuments();

    print("kjndskjl$products");
    lastDocument = querySnapshot.documents[querySnapshot.documents.length - 1];
    products = querySnapshot.documents;
    setState(() {

      isLoading = false;
    });
  }

}
getMorePosts()async{
  print('get more called');
  if(hasMore == false){
    return;
  } if(hasMore == true){
    return;
  }
  hasMore = true;
  for( int i=0; i< foollowingList.length; i++) {
        Query q = await Firestore.instance
          .collection('posts/${foollowingList[i]}/userPosts')
          .orderBy('ownerId')
          .orderBy('timestamp', descending: true)
          .startAfterDocument(lastDocument)
          .limit(documentLimit);
        QuerySnapshot querySnapshot = await q.getDocuments();
        if (querySnapshot.documents.length == 0) {
          hasMore = false;
        }
        lastDocument = querySnapshot.documents[querySnapshot.documents.length - 1];
        products.addAll(querySnapshot.documents);
        setState(() {
          hasMore = false;
    print(products);
        });
  }


}

buildPosts(){
  return
    Column(children: [
      Expanded(
        child: products.length == 0
            ? Center(
          child: Text('No Data...'),
        )
            : ListView.builder(
          controller: _scrollController,
          itemCount: products.length,
          itemBuilder: (context, index) {
            return
              FutureBuilder(
                future: usersRef.document(products[index].data['ownerId']).get(),
                builder: (context, snapshot) {
                  if (!snapshot.hasData) {
                    return circularProgress();
                  }
                  User user = User.fromDocument(snapshot.data);
                  return Column(children: <Widget>[
                    ListTile(
                      leading: GestureDetector(
                        onTap: () => showProfile(context, profileId: user.id),
                        child: CircleAvatar(
                          backgroundImage: CachedNetworkImageProvider(user.photoUrl),
                          backgroundColor: Colors.grey,
                        ),
                      ),
                      title: GestureDetector(
                        onTap: () => showProfile(context, profileId: user.id),
                        child: Text(
                          user.displayName,
                          style: TextStyle(
                            color: kText,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                      subtitle: GestureDetector(
                        onTap: () => showProfile(context, profileId: user.id),
                        child: Text(user.username,
                          style: TextStyle(color: kIcon),),
                      ),),
                    cachedNetworkImage(products[index].data['mediaUrl']),

                    Divider(color: kGrey,),
                  ],

                  );
                },
              );

enter image description here![

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
158 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...