未来的建设者没有得到数据 [英] Future builder is not getting data

查看:29
本文介绍了未来的建设者没有得到数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中使用了 FutureBuilder 来查询 Firestore ,但我一直没有从在 FutureBuilder中指定的函数中获取任何数据代码>.但是有趣的是,函数正在执行并获取数据.它只是不将其传递给 FutureBuilder .

I'm using a FutureBuilder in my code in order to query Firestore but I keep getting no data from the function I specified in the FutureBuilder. But funny enough the function is executing and getting the data. It is just not passing it to the FutureBuilder.

代码:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:garuda_academy_app/Tools/FixedColors.dart';

class CoursePage extends StatefulWidget {
  CoursePage({this.userEmail});
  final String userEmail;
  @override
  _CoursePageState createState() => _CoursePageState();
}

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

  Future<List<DocumentSnapshot>> _getCourses() async {
    List<DocumentSnapshot> userCourses = [];

    await Firestore.instance
        .collection("Users")
        .document(widget.userEmail)
        .get()
        .then((DocumentSnapshot userDetails) {
      List<dynamic> courses = userDetails["Courses"];
      courses.forEach((dynamic course) {
        Firestore.instance
            .collection("Courses")
            .document(course)
            .get()
            .then((DocumentSnapshot courseDetails) {
          userCourses.add(courseDetails);

          print(userCourses.length);
        });
      });
    });

    return userCourses;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: secondaryColor,
      resizeToAvoidBottomInset: false,
      body: FutureBuilder(
        future: _getCourses(),
        builder: (context, AsyncSnapshot<List<DocumentSnapshot>> snapshot) {
          // loading display
          if (snapshot.hasError ||
              (snapshot.connectionState == ConnectionState.waiting)) {
            if (snapshot.hasError) print('${snapshot.error}');
            return Center(
              child: CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation<Color>(primaryColor),
              ),
            );
          }

          print(snapshot.data.length);

          return ListView.builder(
            itemCount: snapshot.data.length,
            itemBuilder: (context, index) {
              return Text(
                snapshot.data[index].documentID,
                style: TextStyle(color: Colors.black),
              );
            },
          );
        },
      ),
    );
  }
}

注意,我打印了两次数据长度以检查是否得到了任何东西,输出是这样的:

Notice I print the data length twice to check if I am getting anything, the output is as such:

0 // the future print
1 // the function print

await 似乎无法正常运行,因为将来似乎会在 await 之前采用 List .

It seems like await is not functioning as it should as the future seems to take the List before await.

有人知道为什么会这样吗?

Does anyone know why this is happening ?

推荐答案

问题是您正在FutureBuilder窗口小部件中调用异步方法.在initState方法中调用该函数,并在一个变量中获取该返回值,然后在FutureBuilder窗口小部件中使用该返回值.

Problem is you are calling async method in inside FutureBuilder Widget. call that function inside initState method and get that return value in one varible and use that in FutureBuilder Widget.

查看此链接以获取更多了解

类似问题

Future<List<DocumentSnapshot>> _course;

void initState()
{
    super.initState();
    course= _getCourses()
}

在FutureBuilder窗口小部件中使用课程变量

use course variable in FutureBuilder Widget

FutureBuilder(
    future: _course,
    builder: write your code)

这篇关于未来的建设者没有得到数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆