Flutter:从Navigator.of(context).pop()更新ValueListenableBuilder

时间:2020-07-16 20:13:08

标签: flutter

使用Navigator.of(context).pop()时如何更新ValueListenableBuilder中的值?使用当前代码,该值仅在导航至页面时更新,而在弹出页面时不会更新。

即:

(ViewFavouritesScreen)_favouriteBooks列表= [1]

Navigator.push

(ViewBookScreen)_favouriteBooks列表= [1]

删除书1

Navigator.pop

(ViewFavouritesScreen)_favouriteBooks列表= [1]

Navigator.pop,然后 Navigator.push

(ViewFavouritesScreen)_favouriteBooks list = [] //列表现在为空

class ViewFavouritesScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Box<Book> _bookBox = Hive.box<Book>('books');
    Box _userPrefsBox = Hive.box('userPrefs');
    
    List<String> _favouriteBooks = _userPrefsBox.get('favouriteBooks') != null
            ? List.from(_userPrefsBox.get('favouriteBooks'))
            : [];
    
    return Scaffold(
      appBar: ClearAppBar(
        title: "Favourites",
      ),
      body: ValueListenableBuilder(
        valueListenable: _userPrefsBox.listenable(),
        builder: (context, Box userPrefs, _) {
          return _favouriteBooks != null || _favouriteBooks.length != 0
          ? SingleChildScrollView(...

我当时以为我需要在弹出时调用setState(),但这似乎不是正确的方法?

不胜感激!

0 个答案:

没有答案