【问题标题】:Flappy search bar : suggestions not updated after Firebase modificationsFlappy 搜索栏:Firebase 修改后未更新建议
【发布时间】:2021-03-10 12:28:47
【问题描述】:

我是 Flutter 的新手,使用这个包时遇到了一些问题:https://pub.dev/packages/flappy_search_bar

我将它与建议一起使用(在搜索栏中没有写入任何内容时提出),并且我有不同的问题,因为在 Firebase 上追加更改时建议列表不会更新。 这是我的代码(在有状态的小部件中):

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&lt;LibraryBook&gt; books 更新得很好,但是 searchBar 的建议并没有遵循这个更新...

有什么想法吗?

This is what the screen looks like

编辑:

first issue when cancelling a search

second issue when deleting an item

third issue when adding a new item (这个不是每次都追加...我不知道为什么)

【问题讨论】:

    标签: firebase flutter searchbar


    【解决方案1】:

    您需要的是状态管理解决方案。我建议查看Provider 包。您需要一个在小部件之间共享的图书模型。

    另外,如果您不熟悉状态管理,请查看这个 Flutter article

    【讨论】:

    • 感谢您的回答,但它并没有解决我的问题...我通过添加一些 GIF 来显示问题来编辑我的问题,也许是由于其他原因.. :(
    • 啊,谢谢分享 GIF。我现在看到您需要一个更强大的解决方案来满足您的需求。请查看我更新的答案。祝你好运。
    猜你喜欢
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2021-05-04
    相关资源
    最近更新 更多