Search code examples
flutterfirebaselistgoogle-cloud-firestore

FLutter when i use a streambuilder to display data from firebase it just says true when on phone


I wish to display a list of events store in firebase firestore and it works well on android emulator and web but when i try it on my real phone it instead just returns the text true to the page. I dont know why because both the emulator and real machine are android. If anyone might know why this happens please help. all of this is in dart of course

class _events_page_state extends State<events_page> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: FirebaseFirestore.instance.collection("Events").snapshots(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.active) {
            if (snapshot.hasData) {
              return Stack(
                children: [
                  ListView.builder(
                      itemCount: snapshot.data!.docs.length,
                      itemBuilder: (context, index) {
                        return ListTile(
                            titleAlignment: ListTileTitleAlignment.center,
                            title: Card(
                              clipBehavior: Clip.hardEdge,
                              child: InkWell(
                                splashColor: Colors.blue.withAlpha(30),
                                child: SizedBox(
                                  width: 350,
                                  height: 200,
                                  child: Stack(
                                    children: [
                                      Positioned(
                                        top: 10,
                                        left: 10,
                                        child: Column(
                                          mainAxisAlignment:
                                              MainAxisAlignment.start,
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              "${snapshot.data!.docs[index]["name"]}",
                                              style: TextStyle(fontSize: 20),
                                            ),
                                            Text(
                                              "${snapshot.data!.docs[index]["description"]}",
                                              style: TextStyle(fontSize: 14),
                                            ),
                                          ],
                                        ),
                                      ),
                                      Positioned(
                                        bottom: 10,
                                        left: 10,
                                        child: Text(
                                          "${snapshot.data!.docs[index]["date"]}",
                                          style: TextStyle(fontSize: 16),
                                        ),
                                      ),
                                      Positioned(
                                        bottom: 10,
                                        right: 10,
                                        child: IconButton.outlined(
                                          onPressed: favorite(),
                                          icon: Icon(
                                            Icons.star,
                                            color: Colors.yellow[700],
                                            size: 24.0,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ));
                      }),
                  Positioned(
                    bottom: 30,
                    right: 30,
                    child: FloatingActionButton(
                      onPressed: () {
                        setState(() {
                          Navigator.of(context).push(
                            MaterialPageRoute(
                              builder: (context) => const add_event_form_page(),
                            ),
                          );
                        });
                      },
                      backgroundColor: Colors.blue,
                      hoverColor: Colors.blue,
                      focusColor: Colors.blue[300],
                      child: const Icon(
                        Icons.add,
                        color: Colors.white,
                      ),
                    ),
                  )
                ],
              );
            } else if (snapshot.hasError) {
              return Center(child: Text("${snapshot.hasError.toString()}"));
            } else {
              return Center(child: Text("an unexpected error has occured"));
            }
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }}

Solution

  • You're getting true because of some error in the streamBuilder, and executing the line

    return Center(child: Text("${snapshot.hasError.toString()}"));
    

    probably some firebase connection issue