ListView没有';t使用StreamBuilder并从Firebase Firestore数据库查询时更新/获取数据
我正在尝试从特定的集合用户中检索数据,如下面的代码所示,但在查询此集合中的用户时,listview没有显示。换句话说,它只显示一个CircularProgressIndicator(),表示找不到任何数据。当我注释掉where查询时,listview呈现得非常完美ListView没有';t使用StreamBuilder并从Firebase Firestore数据库查询时更新/获取数据,listview,flutter,google-cloud-firestore,querying,stream-builder,Listview,Flutter,Google Cloud Firestore,Querying,Stream Builder,我正在尝试从特定的集合用户中检索数据,如下面的代码所示,但在查询此集合中的用户时,listview没有显示。换句话说,它只显示一个CircularProgressIndicator(),表示找不到任何数据。当我注释掉where查询时,listview呈现得非常完美 backgroundColor: MyApp.backgroundColor, body: StreamBuilder<QuerySnapshot>( stream: _
backgroundColor: MyApp.backgroundColor,
body: StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('users')
// .where("chapter", isEqualTo: chapter)
.orderBy('count', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Center(child: CircularProgressIndicator());
return ListView.builder(
itemExtent: 80.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildListItem(context, snapshot.data.documents[index]),
);
},
),
);
return FutureBuilder(
future: _loadChapter(),
builder: (context, snapshot) {
return Scaffold(
backgroundColor: MyApp.backgroundColor,
body: StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('users')
.where("chapter", isEqualTo: chapter)
.orderBy('count', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Center(child: CircularProgressIndicator());
return ListView.builder(
itemExtent: 80.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildListItem(context, snapshot.data.documents[index]),
);
},
),
);
}
);
backgroundColor:MyApp.backgroundColor,
正文:StreamBuilder(
流:_firestore
.collection('用户')
//.其中(“章节”,isEqualTo:章节)
.orderBy('count',降序:true)
.snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData)
返回中心(子项:CircularProgressIndicator());
返回ListView.builder(
项目范围:80.0,
itemCount:snapshot.data.documents.length,
itemBuilder:(上下文,索引)=>
_buildListItem(上下文、快照、数据、文档[索引]),
);
},
),
);
我曾尝试将嵌套Streambuilder和FutureBuilder与Streambuilder结合使用,但这些解决方案都不起作用。对于这样一个关键特性,必须有一个简单的解决方案
请告知我如何同时查询和使用StreamBuilder 如下面我的代码片段所示,这里的技巧是用FutureBuilder包装StreamBuilder。如果你想做任何需要花费时间才能获得的查询,你需要一个FutureBuilder。在我的例子中,需要加载章节,以便listview可以查询它。listview没有使用数据更新的原因是,当listview尝试呈现时,章节为空 注意:如果您正在查询您提前知道的内容,则无需使用FutureBuilder。我测试了一个硬编码的章节,它工作得很好
backgroundColor: MyApp.backgroundColor,
body: StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('users')
// .where("chapter", isEqualTo: chapter)
.orderBy('count', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Center(child: CircularProgressIndicator());
return ListView.builder(
itemExtent: 80.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildListItem(context, snapshot.data.documents[index]),
);
},
),
);
return FutureBuilder(
future: _loadChapter(),
builder: (context, snapshot) {
return Scaffold(
backgroundColor: MyApp.backgroundColor,
body: StreamBuilder<QuerySnapshot>(
stream: _firestore
.collection('users')
.where("chapter", isEqualTo: chapter)
.orderBy('count', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Center(child: CircularProgressIndicator());
return ListView.builder(
itemExtent: 80.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildListItem(context, snapshot.data.documents[index]),
);
},
),
);
}
);
返回FutureBuilder(
未来:_loadChapter(),
生成器:(上下文,快照){
返回脚手架(
backgroundColor:MyApp.backgroundColor,
正文:StreamBuilder(
流:_firestore
.collection('用户')
.其中(“章节”,isEqualTo:章节)
.orderBy('count',降序:true)
.snapshots(),
生成器:(上下文,快照){
如果(!snapshot.hasData)
返回中心(子项:CircularProgressIndicator());
返回ListView.builder(
项目范围:80.0,
itemCount:snapshot.data.documents.length,
itemBuilder:(上下文,索引)=>
_buildListItem(上下文、快照、数据、文档[索引]),
);
},
),
);
}
);
以下是loadChapter方法的外观,以供参考
Future<void> _loadChapter() async {
return await _populateCurrentUser(loggedInUser);
}
Future\u loadChapter()异步{
return wait_populateCurrentUser(loggedInUser);
}
_populateCurrentUser(loggedInUser)基本上查找与当前登录用户链接的文档,并填充全局变量,如章节 您是否在任何地方检查流的错误?如果没有,你应该这样做,因为你的查询可能会失败,错误会给你更多的细节。嗨,我解决了问题!我用StreamBuilder和FutureBuilder包装,这就成功了!很高兴听到你发现了问题。您想发布您的工作代码和更改的解释作为答案吗?这样人们就可以看到你是如何解决这个问题的,随着时间的推移,你可能会赢得一些声誉。当然!现在添加