Flutter:使小部件保持活动状态(streambuilder)

时间:2019-09-05 16:37:12

标签: flutter

我有一个带有网页浏览器的Flutter应用程序(底部是cupertinotabbar)。一切“正常”。但是,当我切换页面时,streambuilders会出现“故障”。 (第一页和第二页上都有一个)(参见视频)。

我认为问题在于,颤振会“破坏”流生成器,然后重新加载它们。

我尝试将keepClientAlive放到有状态的小部件上,但是没有用。 是否有可能保持streambuilder的生命?

这是我的代码:

@override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => false,
      child: Scaffold(
        body: PageView(
          children: <Widget>[
            Discover(currentUser: currentUser),
            Friends(currentUser: currentUser),
            Find(),
          ],
          controller: pageController,
          onPageChanged: onPageChanged,
          physics: NeverScrollableScrollPhysics(),
        ),
        bottomNavigationBar: CupertinoTabBar(
          backgroundColor: Colors.black,
          currentIndex: pageIndex,
          onTap: onTap,
          activeColor: Colors.green,
          items: [
            BottomNavigationBarItem(icon: Icon(FontAwesomeIcons.search)),
            BottomNavigationBarItem(icon: Icon(FontAwesomeIcons.users)),
            BottomNavigationBarItem(icon: Icon(FontAwesomeIcons.biking)),
          ],
        ),
      ),
    );
  }

我的streambuilder类

class BookList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: Firestore.instance.collection('books').snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError)
          return new Text('Error: ${snapshot.error}');
        switch (snapshot.connectionState) {
          case ConnectionState.waiting: return new Text('Loading...');
          default:
            return new ListView(
              children: snapshot.data.documents.map((DocumentSnapshot document) {
                return new ListTile(
                  title: new Text(document['title']),
                  subtitle: new Text(document['author']),
                );
              }).toList(),
            );
        }
      },
    );
  }
}

https://www.youtube.com/watch?v=y2yKhqvkT_A&feature=youtu.be

谢谢!

0 个答案:

没有答案