其中的子句在颤振火力流



我正在设置一个类别选择器,其中按某个图像会更改用户应该看到的类别。 但是,如果用户不选择类别,他应该看到所有项目。

FutureBuilder(
future: _authService.getUserId(),
builder: (context, snapshot) {
if (snapshot.hasData)
return StreamBuilder(
stream: _firestore
.collection('Item')
.where(_category != null ? ('category', isEqualTo: _category) : true)
.snapshots(),

我试图做这样的事情,但它只是给了我一个错误,说它期待在某处有一个括号。

我对此设置的另一个问题是,即使_categoy发生变化,也会更新列表吗? 或者既然流已经构建,它就不会? 在后者中,我将如何使用实际值更新列表?

将其更改为以下内容:

StreamBuilder(
stream: _category != null
? Firestore.instance
.collection('Item')
.where("category", isEqualTo: _category)
.snapshots()
: Firestore.instance.collection('Item').snapshots(),
),

如果_category不为 null,则使用where子句,否则检索Item中的所有内容。

实际答案在这里 https://stackoverflow.com/a/63293409/7198006

那是

不要在构建方法中初始化流

使用 StatefulWidget 并在 State.initState 中初始化流

最新更新