如果未在flutter中进行热重装,则无法加载Firebase数据

时间:2020-08-25 10:09:26

标签: flutter

这是从Firebase检索博客数据的方法。 “ blogs”是用于在listview中存储博客数据的列表,而Blog是博客模型类。

void getBlogs() async{
              var blogsFromDatabase =
                  await Firestore.instance.collection('blogsDatabase').getDocuments();
              for (var blog in blogsFromDatabase.documents) {
                if (blog.data["accept"] != 0) {
                  blogs.add(
                    Blog(
                      title: blog.data["title"],
                      content: blog.data["content"],
                      author: blog.data["author"],
                      pic1: blog.data["picture1"],
                      pic2: blog.data["picture2"],
                    ),
                  );
                }
              }
          }

这是Listview构建器,它从检索到的数据构建博客。它由一个平面按钮组成,其中包含标题和“ picture1”。直到按下热重载后才加载。 请帮忙。

ListView.builder(
          scrollDirection: Axis.vertical,
          shrinkWrap: true,
          padding: const EdgeInsets.all(8),
          itemCount: blogs.length,
          itemBuilder: (BuildContext context, int index) {
            return WillPopScope(
              onWillPop: () async {
                return Future.value(false);
              },
              child: Container(
                child: Center(
                  child: Column(
                    children: <Widget>[
                      Container(
                        height: 350,
                        width: double.infinity,
                        decoration: BoxDecoration(
                          gradient: LinearGradient(
                            begin: Alignment.topLeft,
                            end: Alignment.bottomRight,
                            colors: <Color>[
                              Colors.orange,
                              Colors.red,
                            ],
                          ),
                          borderRadius: BorderRadius.circular(20),
                        ),
                        child: FlatButton(
                          onPressed: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => BlogExpanded(
                                  title: blogs[index].title,
                                  content: blogs[index].content,
                                  pic1: blogs[index].pic1,
                                  pic2: blogs[index].pic2,
                                  author: blogs[index].author,
                                ),
                              ),
                            );
                          },
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              Flexible(
                                child: Text(
                                  blogs[index].title.toUpperCase(),
                                  textAlign: TextAlign.center,
                                  style: kTitleHeadingBlogCard.copyWith(
                                      fontSize: 18),
                                ),
                              ),
                              SizedBox(height: 20),
                              FutureBuilder(
                                builder: (context, snapshot) {
                                  switch (snapshot.connectionState) {
                                    case ConnectionState.none:
                                      return Text('No Data');
                                      break;
                                    case ConnectionState.waiting:
                                      return Container(
                                        width: 200,
                                        height: 200,
                                        child: CircularProgressIndicator(),
                                      );
                                      break;
                                    case ConnectionState.active:
                                      return Container(
                                        width: 200,
                                        height: 200,
                                        child: CircularProgressIndicator(),
                                      );
                                      break;
                                    case ConnectionState.done:
                                      return Container(
                                        width: 200,
                                        height: 200,
                                        child: Image.network(
                                          snapshot.data.toString(),
                                          fit: BoxFit.contain,
                                        ),
                                      );
                                      break;
                                  }
                                  return Container(
                                    width: 200,
                                    height: 200,
                                    child: CircularProgressIndicator(),
                                  );
                                },
                                future: _getImage(blogs[index].pic1),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
        ),

2 个答案:

答案 0 :(得分:0)

void getBlogs() async{

请勿编写不返回async的{​​{1}}函数。我可以说这是行不通的,因为您不能Future,也不能将其交给await

即使您不退货,也请使用FutureBuilder

Future

将来,您将不得不使用Future<void> getBlogs() async { ,以便您可以等待数据实际到达。您以后已经在使用图像了,所以我假设您知道该怎么做。

如果您需要更新知识,请随时阅读What is a Future and how do I use it?

答案 1 :(得分:0)

您需要在将项目添加到列表后设置状态。

void getBlogs() async{
              var blogsFromDatabase =
                  await Firestore.instance.collection('blogsDatabase').getDocuments();
              for (var blog in blogsFromDatabase.documents) {
                if (blog.data["accept"] != 0) {
                  blogs.add(
                    Blog(
                      title: blog.data["title"],
                      content: blog.data["content"],
                      author: blog.data["author"],
                      pic1: blog.data["picture1"],
                      pic2: blog.data["picture2"],
                    ),
                  );

                  if(mounted){           
                  setState(() {            // Add these lines to your code
          
                       });
                  }
  
                }
              }
          }