Flutter使用flappy_search_bar时Firebase改动后搜索建议不更新
flappy_search_bar 建议列表同步异常问题
开发背景
我是Flutter开发新手,在使用flappy_search_bar组件实现功能时遇到问题:已配置搜索栏无输入内容时的默认展示建议列表功能,但当Firebase端发生数据变更后,搜索栏的建议列表始终无法同步更新。
相关实现代码
以下是我在StatefulWidget中编写的核心实现代码:
SearchBarController _searchBarController = SearchBarController(); List<DocumentSnapshot> documents = []; List<LibraryBook> books = []; @override void initState() { super.initState(); FireHelper().libraryBooksFrom(widget.user.uid).listen((event) { setState(() { books = getLibraryBooks(documents); }); }); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { return StreamBuilder<QuerySnapshot>( stream: FireHelper().libraryBooksFrom(widget.user.uid), builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { if (snapshot.hasData) { documents = snapshot.data.docs; books = getLibraryBooks(documents); return Scaffold( backgroundColor: white, floatingActionButton: FloatingActionButton( onPressed: () => setState() { AlertHelper().addBookToLibrary(context); }, child: Icon(Icons.add), backgroundColor: pointer, ), floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, body: Column( children: <Widget>[ SafeArea( bottom: false, child: Row( children: <Widget>[ BackButton(color: Colors.black), MyText(" Ma bibliothèque", color: baseAccent) ], ) ), Expanded( child: SearchBar( searchBarPadding: EdgeInsets.symmetric(horizontal: 10), onSearch: (inputText) => searchBooks(inputText), suggestions: books, minimumChars: 2, crossAxisCount: 2, onCancelled: () => searchBooks(null), crossAxisSpacing: 0, onError: (error) => ErrorWidget(error), searchBarController: _searchBarController, hintText: "Chercher un livre...", cancellationWidget: Text("Annuler"), emptyWidget: Text("Aucune correspondance"), onItemFound: (item, int index) { if (item is LibraryBook) { return BookLibraryTile(item, null); } else { return Text("Aucune correspondance"); } } ) ) ], ), ); } else { return LoadingCenter(); } } ); }
观测到的异常现象
测试过程中发现,当Firebase端发生数据变更时,本地维护的List<LibraryBook> books列表可以被正常更新,但SearchBar组件展示的建议内容却没有跟随列表更新同步刷新。
具体可复现的异常场景包括:
- 取消搜索操作时出现页面显示异常
- 删除列表项时出现页面显示异常
- 新增列表项时偶发出现页面显示异常,该问题并非每次操作都会触发,暂未明确触发规律
内容的提问来源于stack exchange,提问作者LDX
相关产品推荐
相关产品推荐

