在firebase中,我有一个名为selectedPlace的字段。在这个领域,餐厅,咖啡馆或居酒屋都可以进入。
我的代码中还有一个下拉按钮,包含了Restaurant、Cafe和Izakaya。我使用了一个streambuilder来显示posts集合中的所有文档,并使用.where()来整理selectedPlace。代码如下:
class MatjipPage extends StatefulWidget {
MatjipPage({super.key});
@override
State<MatjipPage> createState() => _MatjipPageState();
}
class _MatjipPageState extends State<MatjipPage> {
@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
String? _selectedPlace;
bool _filterActive = false;
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
collapsedHeight: 150,
backgroundColor: Colors.white,
floating: false,
pinned: true,
expandedHeight: 10,
flexibleSpace: Container(
margin: EdgeInsets.only(top: 40),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: () {},
icon: Icon(
Icons.arrow_back_ios_new,
color: Colors.grey,
)),
IconButton(
onPressed: () {},
icon: Icon(
Icons.search,
color: Colors.black,
size: 35,
weight: 50,
))
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
'Restaurant Review',
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 18),
),
IntrinsicHeight(
child: Row(
children: [
SizedBox(
width: 83,
child: DropdownBar(
items: ['All', 'Restaurant', 'Cafe', 'Izakaya'],
hintText: 'Sort By',
onChanged: (newValue) {
setState(() {
if (newValue == 'All') {
_filterActive = false;
} else {
_filterActive = true;
_selectedPlace = newValue;
}
print('Selected Place: $_selectedPlace');
print('Filter Active: $_filterActive');
});
},
),
)],
),
)
],
),
SizedBox(
width: MediaQuery.of(context).size.width,
height: 3,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.topRight,
colors: [
Colors.white,
primaryColor,
Colors.white
])),
),
),
Padding(
padding: EdgeInsets.only(top: 13, left: 20),
child: Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 10),
child: SizedBox(
width: 90,
child: DropdownBarRound(
items: ['Breakfast', 'Lunch', 'Dinner'],
hintText: 'Time',
)),
),
SizedBox(
width: 95,
child: DropdownBarLoc(
items: ['SHUBUYA', 'SHINJUKU', 'WASEDA'],
hintText: 'Location',
)),
SizedBox(),
],
),
),
],
),
),
),
SliverList(
delegate: SliverChildListDelegate([
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('posts')
.where('selectedPlace', isEqualTo: _filterActive ? _selectedPlace : null)
.snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>
snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return Container(
height: MediaQuery.of(context).size.height * 0.8,
child: ListView.builder(
itemCount: snapshot.data?.docs.length,
itemBuilder: (ctx, index) {
List<String> postUrls = snapshot.data?.docs[index]
.get('postUrl')
.split(',\n');
return Container(
padding: EdgeInsets.only(bottom: 25),
margin: EdgeInsets.symmetric(
horizontal:
width > webScreenSize ? width * 0.3 : 0,
vertical: width > webScreenSize ? 15 : 0,
),
child: PostCard(
snap: snapshot.data?.docs[index].data(),
postUrls: postUrls,
),
);
}),
);
},
)
]),
),
],
),
floatingActionButton: Padding(
padding: const EdgeInsets.only(right: 16.0, bottom: 16.0),
child: FloatingActionButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => AddPostScreen()));
},
child: Icon(Icons.add),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
);
_selectedPlace
和_filterActive
工作正常,因为我用print()检查了它,但是帖子不会被整理出来,所有的文档都会显示在模拟器中。如果有人能帮我解决这个问题,我将不胜感激。非常感谢
1条答案
按热度按时间bvpmtnay1#
将
State
类中的_filterActive
和_selectedPlace
作为成员属性。你已经在build
方法中初始化了它。即使你改变了
_filterActive
的值。再次重建后仍为false
。