使用 StreamBuilder 和从 Firebase Firestore 数据库查询时,ListView 没有 update/get 数据
ListView doesn't update/get data when using StreamBuilder and Querying from Firebase Firestore database
我正在尝试从特定集合 users 中检索数据,如下面的代码所示,但是当我查询此集合中的用户时,列表视图不显示向上。换句话说,它只显示一个 CircularProgressIndicator() 表示没有数据可以找到。在我注释掉 where 查询的那一刻,列表视图完美呈现。
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]),
);
},
),
);
我尝试将嵌套 StreamBuilder 和 FutureBuilder 与 Streambuilder 结合使用,但这些解决方案中 none 有效。必须有一个简单的解决方案来解决如此重要的功能。
请告诉我如何同时查询和使用 StreamBuilder。
如下面的代码片段所示,这里的技巧是用 FutureBuilder 包装 StreamBuilder。如果您想进行任何涉及需要时间获取的查询,则需要 FutureBuilder。在我的例子中,需要加载 chapter 以便列表视图可以查询它。 listview 没有更新数据的原因是当 listview 试图呈现时 chapter 为 null。
注意:如果您要查询您提前知道的内容,则不需要 FutureBuilder。我针对一个硬编码的章节对此进行了测试,它运行良好。
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]),
);
},
),
);
}
);
这里是 loadChapter 方法的样子,供参考。
Future<void> _loadChapter() async {
return await _populateCurrentUser(loggedInUser);
}
_populateCurrentUser(loggedInUser) 基本上找到与当前登录用户链接的文档并填充全局变量,例如 章节 。
我正在尝试从特定集合 users 中检索数据,如下面的代码所示,但是当我查询此集合中的用户时,列表视图不显示向上。换句话说,它只显示一个 CircularProgressIndicator() 表示没有数据可以找到。在我注释掉 where 查询的那一刻,列表视图完美呈现。
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]),
);
},
),
);
我尝试将嵌套 StreamBuilder 和 FutureBuilder 与 Streambuilder 结合使用,但这些解决方案中 none 有效。必须有一个简单的解决方案来解决如此重要的功能。
请告诉我如何同时查询和使用 StreamBuilder。
如下面的代码片段所示,这里的技巧是用 FutureBuilder 包装 StreamBuilder。如果您想进行任何涉及需要时间获取的查询,则需要 FutureBuilder。在我的例子中,需要加载 chapter 以便列表视图可以查询它。 listview 没有更新数据的原因是当 listview 试图呈现时 chapter 为 null。
注意:如果您要查询您提前知道的内容,则不需要 FutureBuilder。我针对一个硬编码的章节对此进行了测试,它运行良好。
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]),
);
},
),
);
}
);
这里是 loadChapter 方法的样子,供参考。
Future<void> _loadChapter() async {
return await _populateCurrentUser(loggedInUser);
}
_populateCurrentUser(loggedInUser) 基本上找到与当前登录用户链接的文档并填充全局变量,例如 章节 。