Flutter中的类“ List <DocumentSnapshot>”没有实例方法“ call”

时间:2020-05-17 08:19:10

标签: firebase flutter dart

我的firestore数据库看起来像这样 enter image description here

我想做的是,我使用相同的uid制作所有文档的Future列表,然后使用FutureBuilder制作ListView。

给我错误的部分是

body: FutureBuilder(
        future: listBuilder(),
        builder: (context, snapshot) {
          if(snapshot.hasData == null){
            return Container();
          }
          return ListView(
            padding: const EdgeInsets.only(top: 20.0),
            children: snapshot.data((data) => _buildListItem(context, data)).toList(),
          );
        },
      ),
Future listBuilder() async {
    final FirebaseUser user = await FirebaseAuth.instance.currentUser();
    final String uid = user.uid;
    List<DocumentSnapshot> list = [];
    Firestore.instance
        .collection('entries')
        .where("uid", isEqualTo: uid)
        .snapshots()
        .listen((data) => data.documents.forEach((doc) => list.add(doc)));

    print("------------------------------------------------------------------");
    return list;
  }
Widget _buildListItem(BuildContext context, DocumentSnapshot snapshot) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16.0),
      child: InkWell(
        child: Row(
          children: <Widget>[
            Expanded(
              child: ListTile(
                leading: Text(
                  snapshot.data['type'],
                  style: TextStyle(
                    fontSize: 30,
                  ),
                ),
                title: Text(snapshot.data['description'],
                    style: TextStyle(
                      fontSize: 30,
                    )),
                subtitle: Text(snapshot.data['time'].toDate().toString(),
                    style: TextStyle(
                      fontSize: 15,
                    )),
                trailing: Text(snapshot.data['value'].toString(),
                    style: TextStyle(
                      fontSize: 25,
                    )),
              ), 
            )
          ],
        ),
      ),
    );
  }

这是错误,

Class 'List<DocumentSnapshot>' has no instance method 'call'.
Receiver: Instance(length:3) of '_GrowableList'
Tried calling: call(Closure: (dynamic) => Widget)

谢谢!

1 个答案:

答案 0 :(得分:1)

使用map方法。

     ...
children: snapshot.data.map((data)=>_buildListItem(context,data)).toList(),
     ...