FLutter 当我使用 Streambuilder 显示来自 Firebase 的数据时,它在手机上只是说 true

问题描述 投票:0回答:1

我希望显示 firebase firestore 中存储的事件列表,它在 android 模拟器和网络上运行良好,但当我在真实手机上尝试它时,它只是将文本 true 返回到页面。我不知道为什么,因为模拟器和真机都是android。如果有人知道为什么会发生这种情况,请帮忙。当然,所有这些都在 dart 中

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.has`your text`Error) {
              return Center(child: Text("${snapshot.hasError.toString()}"));
            } else {
              return Center(child: Text("an unexpected error has occured"));
            }
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }}
flutter firebase list google-cloud-firestore
1个回答
0
投票

由于

streamBuilder
中出现一些错误,并且执行该行

,您得到了正确的答案
return Center(child: Text("${snapshot.hasError.toString()}"));

可能是一些 Firebase 连接问题

© www.soinside.com 2019 - 2024. All rights reserved.