Flutter FireStore

我正在使用Firebase Cloud Firestore和我的Flutter应用程序 . 我面临一个问题,即当FireStore发生变化时我的小部件不会更新,它会强制我关闭屏幕并再次打开它 .

这是我的streambuiler:

_layout() {
    return StreamBuilder(
      stream: Firestore.instance
          .collection(Constants.usersNode)
          .document(Constants.dummyUserId)
          .snapshots(),
      builder: (context, snapShot) {
        if (!snapShot.hasData)
          return Center(
            child: CircularProgressIndicator(),
          );
        customer = new Customer.fireStore(snapShot.data);
        // print("here: ${customer.fullName}");
        return new ListView(
          children: _widgets(context, snapShot.data),
        );
      },
    );
  }

这是我添加小部件的模块:

_widgets(BuildContext context, DocumentSnapshot document) {
    widgets.clear();
    print(" >> ${customer.fullName}");
    widgets.add(new Card(
      child: new Column(
        children: <Widget>[
          new Text(
            customer.fullName,
            style: Theme.of(context).textTheme.title,
          ),
          new SizedBox(
            height: 8.0,
          ),
          new Container(
            height: 1.0,
            width: 100.0,
            color: Colors.grey,
          ),
          new SizedBox(
            height: 8.0,
          ),
          new ListTile(
            leading: Icon(
              MyIcons.phone_call_2,
              color: Colors.black,
            ),
            title: new Text(customer.cell),
          ),
          new SizedBox(
            height: 8.0,
          ),
          new Align(
            alignment: Alignment.bottomRight,
            child: new Padding(
              padding: const EdgeInsets.all(8.0),
              child: new InkWell(
                onTap: () {
                  print("name: ${customer.fullName}");
                  Navigator.push(
                      context,
                      new MaterialPageRoute(
                          builder: (context) => new EditProfileScreen(
                                customer: customer,
                              )));
                },
                child: new Icon(
                  Icons.edit,
                  color: Colors.yellow,
                ),
              ),
            ),
          )
        ],
      ),
    ));       

    return widgets;
  }

但是,控制台会实时打印更新,但小部件仍会显示旧数据 . 我究竟做错了什么?我想每次检测到更改时调用setState(),但是从文档中,Streambuilder似乎会照顾它 .

回答(1)

3 years ago

实例变量 widgets 似乎没有用,可能会导致问题 . 每次都要制作一个新的清单 .

_widgets(BuildContext context, DocumentSnapshot document) {
  List<Widget> widgets = [];
  print(" >> ${customer.fullName}");