使用firebase进行颤振分页不适合我

使用firebase进行颤振分页不适合我,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我尝试了两种可能性,但firebase数据未存储在产品列表中。应用程序页面不返回任何数据,因为产品列表中没有任何内容。愚弄列表成功返回数据。我尝试的第二种可能性和控制台日志位于下面的pastebin链接中: List-boodlowinglist=[]; 列表_listOfImages=[]; 列出产品=[];//商店进货 bool isLoading=true;//跟踪是否正在提取产品 bool hasMore=true;//是否提供更多产品的标志 int documentLimit=10;

我尝试了两种可能性,但firebase数据未存储在产品列表中。应用程序页面不返回任何数据,因为产品列表中没有任何内容。愚弄列表成功返回数据。我尝试的第二种可能性和控制台日志位于下面的pastebin链接中:

List-boodlowinglist=[];
列表_listOfImages=[];
列出产品=[];//商店进货
bool isLoading=true;//跟踪是否正在提取产品
bool hasMore=true;//是否提供更多产品的标志
int documentLimit=10;//每个请求要获取的文档
DocumentSnapshot lastDocument;//从中提取下10条记录的最后一个文档的标志
ScrollController_ScrollController=ScrollController()/
void initState(){
super.initState();
getFfollowing();
getPosts();
_scrollController.addListener((){
double maxScroll=\u scrollController.position.maxScrollExtent;
双currentScroll=\u scrollController.position.pixels;
double delta=MediaQuery.of(context).size.height*0.20;
if(maxScroll-currentScroll doc.documentID).toList();
});
}
getPosts()异步{
for(int i=0;ishowProfile(上下文,profileId:user.id),
孩子:圆环星(
backgroundImage:CachedNetworkImageProvider(user.photoUrl),
背景颜色:颜色。灰色,
),
),
标题:手势检测器(
onTap:()=>showProfile(上下文,profileId:user.id),
子:文本(
user.displayName,
样式:TextStyle(
颜色:kText,
fontWeight:fontWeight.bold,
),
),
),
字幕:手势检测器(
onTap:()=>showProfile(上下文,profileId:user.id),
子项:文本(user.username,
样式:TextStyle(颜色:kIcon),),
),),
cachedNetworkImage(产品[index]。数据['mediaUrl']),
分隔器(颜色:kGrey,),
],
);
},
);
![

在您的第一个代码中

getMorePosts()async{
  print('get more called');
  if(hasMore == false){
    return;
  } if(hasMore == true){
    return;
  }
  //The rest is dead code, you always return if hasMore is true or false
  ....
}
在您的代码片段中,它似乎实际上填充了prodcuts列表,但用户为null
(getter'id'被调用为null)
,您确定
user user=user.fromDocument(snapshot.data);
正确解析了数据吗?请尝试更改
showProfile(上下文,profileId:user?.id?.1)
以测试它(我不知道id是否应该是int、String等,但如果是String,请使用一些字符串“Test”更改1)

更新

不要在
initState
中运行getPost,而是在
getFfollowing

void initState() {
  super.initState();
  getFfollowing();
  //getPosts(); this could run after getFfollowing ends, where the list is empty
  _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();
  });
  if(foollowingList.isNotEmpty) await getPosts(); //run it here when you're sure the future is completed and the list is full
}

但是getPosts()本身并没有返回任何数据。不,我在产品中执行了一个print命令。列表是elmpt。我看到getPosts与getMorePosts混淆了,在getPosts中它是否打印(“kjndskjl$products”);?是的,列表是空的,并且在调试getFfollowing does愚弄列表时,在方法结束时列表不是空的?我这样做的
onPressed:(){print(傻瓜列表);print(getPosts());
会在应用程序上获取帖子和显示。为什么会出现这种情况?即使在启动
getPosts()
之后,它也不会在未按下按钮的情况下执行。这是因为setState处于forloop中吗?
void initState() {
  super.initState();
  getFfollowing();
  //getPosts(); this could run after getFfollowing ends, where the list is empty
  _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();
  });
  if(foollowingList.isNotEmpty) await getPosts(); //run it here when you're sure the future is completed and the list is full
}
getPosts()async{
  setState(() => isLoading = true);
  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);
    QuerySnapshot querySnapshot = await q.getDocuments();

    print("kjndskjl$products");
    lastDocument = querySnapshot.documents[querySnapshot.documents.length - 1];
    products = querySnapshot.documents;
    
  }
  setState(() => isLoading = false);
}