Otra condición no funciona en Flutter usando Cloud Firestore

Oct 21 2020

Mi condición if funciona correctamente y está recuperando datos. Pero cuando los datos son nulos, quiero mostrar un texto en Center (). Texto que indica que aún no se han agregado ofertas. Pero no muestra nada cuando los datos son nulos. Probé todo. Pero la ejecución nunca se va a otro lugar y else if condiciones. ¿Alguien puede arrojar algo de luz sobre lo que me estoy perdiendo?

    child: StreamBuilder(
                                stream: Firestore.instance.collection('BidList').where('PostID',isEqualTo:args.PostId).snapshots(),
                                // ignore: missing_return
                                builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){

                                  if(snapshot.hasData){
                                    return ListView.builder(
                                        itemCount: snapshot.data.documents.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(

                        child: Column(
                          children: <Widget>[
                            Container(
                              // margin: EdgeInsets.only(top: 30),
                              child: Row(
                                mainAxisAlignment:
                                MainAxisAlignment.center,
                                children: <Widget>[
                                  Container(
                                    //color: Colors.grey[500],
                                    color: Colors.teal[700],
                                    width: 300,
                                    height: 25,
                                    child: Center(
                                      child: Text(
                                        'Offers List',
                                        style: TextStyle(
                                            fontSize: 18,
                                            fontWeight:
                                            FontWeight.bold,
                                            color: Colors.white),
                                      ),
                                    ),
                                  )
                                ],
                              ),
                            ),
                            ListTile(
                              title: Text(
                                snapshot.data.documents[index].data['Name']
                                    .toString()
                                    .toUpperCase(),
                                style: TextStyle(fontSize: 14, color: Colors
                                    .black, fontWeight: FontWeight.bold),
                              ),
                              leading: CircleAvatar(
                                //backgroundImage: NetworkImage(w),
                                // backgroundColor: Colors.blueGrey,
                                backgroundImage: CachedNetworkImageProvider(data
                                    .toString()),
                              ),
                              // subtitle: Text(username),
                              //subtitle: Text(accountCreated.toString()),
                              subtitle: Row(
                                children: [
                                  Text('Number: ', style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 10),),
                                  Text(
                                    snapshot.data.documents[index]
                                        .data['Number']
                                        .toString()
                                        .toUpperCase(),
                                    style: TextStyle(
                                        fontSize: 12, color: Colors.black45),
                                  ),
                                ],
                              ),
                              //trailing: Text(timeago.format(timestamp.toDate())),
                              trailing: Container(
                                margin: EdgeInsets.all(10),
                                child: Column(
                                  children: [
                                    Text('Bid', style: TextStyle(
                                        fontWeight: FontWeight.bold),),
                                    Text(
                                      snapshot.data.documents[index].data['Bid']
                                          .toString()
                                          .toUpperCase(),
                                      style: TextStyle(fontSize: 12),),
                                  ],
                                ),
                              ),
                            ),
                            Divider(),
                          ],
                        ),
                      );
                    }
                                    );
                                  }
                                  else if(snapshot.hasError || snapshot == null) {
                                    return Container(
                                      child: Column(
                                        children: <Widget>[
                                          Container(
                                            // margin: EdgeInsets.only(top: 30),
                                            child: Row(
                                              mainAxisAlignment:
                                              MainAxisAlignment.center,
                                              children: <Widget>[
                                                Container(
                                                  //color: Colors.grey[500],
                                                  color: Colors.teal[700],
                                                  width: 300,
                                                  height: 25,
                                                  child: Center(
                                                    child: Text(
                                                      'Offers List',
                                                      style: TextStyle(
                                                          fontSize: 18,
                                                          fontWeight:
                                                          FontWeight.bold,
                                                          color: Colors.white),
                                                    ),
                                                  ),
                                                )
                                              ],
                                            ),
                                          ),
                                          Center(child: Text('No Reviews Yet'),),
                                          Divider(),
                                        ],
                                      ),
                                    );
//
                                  }
                                  else{
                                    return Container(
                                      child: Column(
                                        children: <Widget>[
                                          Container(
                                            // margin: EdgeInsets.only(top: 30),
                                            child: Row(
                                              mainAxisAlignment:
                                              MainAxisAlignment.center,
                                              children: <Widget>[
                                                Container(
                                                  //color: Colors.grey[500],
                                                  color: Colors.teal[700],
                                                  width: 300,
                                                  height: 25,
                                                  child: Center(
                                                    child: Text(
                                                      'Offers List',
                                                      style: TextStyle(
                                                          fontSize: 18,
                                                          fontWeight:
                                                          FontWeight.bold,
                                                          color: Colors.white),
                                                    ),
                                                  ),
                                                )
                                              ],
                                            ),
                                          ),
                                          Center(child: Text('No Reviews Yet'),),
                                          Divider(),
                                        ],
                                      ),
                                    );
                                  }
                                },
                              ),

Respuestas

1 Reign Oct 21 2020 at 06:56

Si su colección no tiene documentos o datos, puede agregar esta condición.

if(snapshot.hasData){
   if (snapshot.data.docs.length != 0) {
        return ListView.builder();
      } else {
        return Text("EMPTY DOCUMENTS");
      } 
}
GunJack Oct 21 2020 at 07:19

Intente usar en snapshot.data == nulllugar de instantánea == null

if(snapshot.hasError || snapshot.data == null)