firestore快照在flutter中返回null

时间:2020-07-18 06:46:12

标签: flutter google-cloud-firestore

我正在尝试使用FutureBuilder在Flutter的GroupedListView中显示来自云存储的结果。但是快照始终返回null。我不知道问题出在哪里。 Cloud Firestore中有数据,但未显示在小部件中。我的代码有什么问题。谢谢。

这是未来的方法:

  Future getList() async {
final FirebaseUser user = await _auth.currentUser();
final uid = user.uid;

var myCode = await Firestore.instance.collection('profiles').where('Uid', isEqualTo: uid).getDocuments();

await Firestore.instance
    .collection('profiles')
    .where('Role', isEqualTo: 'chef')
    .where('PartnerCode', isEqualTo: myCode.documents[0].data['code'])
    .getDocuments().then((ds) async {
  if (ds != null) {
      for (var b = 0; b < ds.documents.length; b++) {
        await Firestore.instance.collection('orders').getDocuments().then((orders) async {
          if (orders != null) {
            for (var i = 0; i < orders.documents.length; i++) {
              return await Firestore.instance
                  .collection('orders').document(
                  orders.documents[i].documentID).collection(
                  ds.documents[b].data['Uid'])
                  .where('Stat', isEqualTo: 'delivered')
                  .getDocuments();
   

这是FutureBuilder:

body: FutureBuilder(
        future: getList(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
          if (snapshot.connectionState == ConnectionState.none) {
            return Center(child: Text('لا توجد بيانات'));
          }
          if (snapshot.hasError) {
            return new Text('Error: ${snapshot.error}');
          }

          if(snapshot.data == null) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
      
            return Scaffold(
                appBar: AppBar(
                  title: Center(
                      child: Container(
                        color: Color(0xff20b198),
                        child: Text(
                          "  مجموع المستحقات : $all ",
                          style: TextStyle(
                              color: Colors.white, fontSize: 16),
                        ),
                      )
                  ),
                  backgroundColor: Colors.white,
                ),

                body: GroupedListView<dynamic, String>(
                  elements: snapshot.data.documents,
                  groupBy: (element) => element['Date'],
            groupSeparatorBuilder: (String groupByValue) => Text(groupByValue),
            itemBuilder: (c, element) {
              return Card(
                elevation: 8.0,
                margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
                child: Container(
                  child: ListTile(
                    contentPadding:
                    EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
                    title: Text(element['kitchenSales'].toDouble() * 0.02.toString()),
                    trailing: Icon(Icons.attach_money)),
         

1 个答案:

答案 0 :(得分:0)

您不能在一个查询中多次使用isEqualTo:(请参见下面的代码)。

我敢肯定,您可能会因此而出错。 因此,这就是您获得空快照的原因。

await Firestore.instance
    .collection('profiles')
    .where('Role', isEqualTo: 'chef')
    .where('PartnerCode', isEqualTo: myCode.documents[0].data['code'])

解决方案: 您需要更改查询以获取数据。

有关查询的更多信息,您可以检查Firebase docs