Asynchronous 如何转换未来<;int>;到int?

Asynchronous 如何转换未来<;int>;到int?,asynchronous,flutter,google-cloud-firestore,async-await,future,Asynchronous,Flutter,Google Cloud Firestore,Async Await,Future,所以我尝试使用fl_图表插件显示一个饼图。正在从firestore检索图表的数据。我有一个用于显示数据的功能: List<PieChartSectionData> showSection(AsyncSnapshot<QuerySnapshot> snapshot) { return List.generate(length, (i) { final isTouched = i == touchedIndex; final double fo

所以我尝试使用fl_图表插件显示一个饼图。正在从firestore检索图表的数据。我有一个用于显示数据的功能:

List<PieChartSectionData> showSection(AsyncSnapshot<QuerySnapshot> snapshot) {
    return List.generate(length, (i) {
      final isTouched = i == touchedIndex;
      final double fontSize = isTouched ? 25 : 16;
      final double radius = isTouched ? 60 : 50;
      return PieChartSectionData(
        color: Color(int.parse(cerealData[i].colorVal)),
        value: cerealData[i].rating,
        title: cerealData[i].rating.toString(),
        radius: radius,
        titleStyle: TextStyle(
            fontSize: fontSize,
            fontWeight: FontWeight.bold,
            color: const Color(0xffffffff)),
      );
    });
  }
列表显示部分(异步快照快照){
返回列表。生成(长度,(i){
最终isTouched=i==touchedIndex;
最终双fontSize=isTouched?25:16;
最终双半径=i接触?60:50;
返回分区数据(
color:color(int.parse(cerealData[i].colorVal)),
值:Cereladata[i]。额定值,
标题:Cereladata[i].rating.toString(),
半径:半径,
标题样式:文本样式(
fontSize:fontSize,
fontWeight:fontWeight.bold,
颜色:常量颜色(0xffffffff)),
);
});
}
generate()将int作为参数。因为我正在显示实时数据,所以我正在尝试获取收藏中存在的文档数。为此,我有一个名为getLength()的函数:

void getLength(异步快照快照)异步{
length=wait Firestore.instance.collection('score').snapshots().length;
天竺葵=
snapshot.data.documents.map((e)=>grane.fromJson(e.data)).toList();
}
但是,当我运行代码时,我得到:

 Another exception was thrown: type 'Future<int>' is not a subtype of type 'int'
引发了另一个异常:“Future”类型不是“int”类型的子类型
整个代码:

class _FlChartPageState extends State<FlChartPage> {
  int touchedIndex;
  var length;
  List<Cereal> cerealData;

  void getLength(AsyncSnapshot<QuerySnapshot> snapshot) async {
    length = await Firestore.instance.collection('cereal').snapshots().length;
    cerealData =
        snapshot.data.documents.map((e) => Cereal.fromJson(e.data)).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder<QuerySnapshot>(
          stream: Firestore.instance.collection('cereal').snapshots(),
          builder: (context, snapshot) {
            getLength(snapshot);
            if (!snapshot.hasData)
              return CircularProgressIndicator();
            else {
              return PieChart(PieChartData(
                  pieTouchData: PieTouchData(touchCallback: (pieTouchResponse) {
                    setState(() {
                      if (pieTouchResponse.touchInput is FlLongPressEnd ||
                          pieTouchResponse.touchInput is FlPanEnd) {
                        touchedIndex = -1;
                      } else {
                        touchedIndex = pieTouchResponse.touchedSectionIndex;
                      }
                    });
                  }),
                  borderData: FlBorderData(show: false),
                  sectionsSpace: 0,
                  centerSpaceRadius: 40,
                  sections: showSection(snapshot)));
            }
          }),
    );
  }

  List<PieChartSectionData> showSection(AsyncSnapshot<QuerySnapshot> snapshot) {
    return List.generate(length, (i) {
      final isTouched = i == touchedIndex;
      final double fontSize = isTouched ? 25 : 16;
      final double radius = isTouched ? 60 : 50;
      return PieChartSectionData(
        color: Color(int.parse(cerealData[i].colorVal)),
        value: cerealData[i].rating,
        title: cerealData[i].rating.toString(),
        radius: radius,
        titleStyle: TextStyle(
            fontSize: fontSize,
            fontWeight: FontWeight.bold,
            color: const Color(0xffffffff)),
      );
    });
  }
}

class\u FlChartPageState扩展状态{
int-touchedIndex;
变异长度;
列明cerealData;
void getLength(异步快照快照)异步{
length=wait Firestore.instance.collection('score').snapshots().length;
天竺葵=
snapshot.data.documents.map((e)=>grane.fromJson(e.data)).toList();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:StreamBuilder(
流:Firestore.instance.collection('graines').snapshots(),
生成器:(上下文,快照){
getLength(快照);
如果(!snapshot.hasData)
返回循环ProgressIndicator();
否则{
返回PieChart(PieChartData(
pieTouchData:pieTouchData(touchCallback:(pieTouchResponse){
设置状态(){
如果(pieTouchResponse.touchInput为FlLongPressEnd||
pieTouchResponse.touchInput为FlPanEnd){
touchedIndex=-1;
}否则{
touchedIndex=pieTouchResponse.touchedSectionIndex;
}
});
}),
borderData:FlBorderData(显示:false),
分区空间:0,
阿迪乌斯:40,
部分:showSection(快照));
}
}),
);
}
列表显示部分(异步快照快照){
返回列表。生成(长度,(i){
最终isTouched=i==touchedIndex;
最终双fontSize=isTouched?25:16;
最终双半径=i接触?60:50;
返回分区数据(
color:color(int.parse(cerealData[i].colorVal)),
值:Cereladata[i]。额定值,
标题:Cereladata[i].rating.toString(),
半径:半径,
标题样式:文本样式(
fontSize:fontSize,
fontWeight:fontWeight.bold,
颜色:常量颜色(0xffffffff)),
);
});
}
}
我在某个地方读到,等待未来可以摆脱未来。但这在这里不起作用。 我该如何解决这个问题


编辑:如果我只是在List.generate()中传递文档的数量而不是长度,它就会起作用。但是,如果集合发生更改,这将不起作用。那么如何将Future转换为int呢

我认为你没有得到文档的长度,如果你想得到文档的长度,你得到的是快照的长度:

QuerySnapshot querySnapshot = await Firestore.instance.collection('cereal').getDocuments();
    int length = querySnapshot.documents.length;


我认为你没有得到文档的长度,如果你想得到文档的长度,你得到的是快照的长度:

QuerySnapshot querySnapshot = await Firestore.instance.collection('cereal').getDocuments();
    int length = querySnapshot.documents.length;


在get getLength函数中,您试图获取的长度实际上是返回future的异步任务,因此您将得到以下错误

用下面的方法改变你的方法

getLength()async{
 Firestore.instance.collection('cereal').snapshots().length.then((len){
  length = len;
  cerealData =
        snapshot.data.documents.map((e) => Cereal.fromJson(e.data)).toList(); 
  });
}

在get getLength函数中,您试图获取的长度实际上是返回future的异步任务,因此您将得到以下错误

用下面的方法改变你的方法

getLength()async{
 Firestore.instance.collection('cereal').snapshots().length.then((len){
  length = len;
  cerealData =
        snapshot.data.documents.map((e) => Cereal.fromJson(e.data)).toList(); 
  });
}

我确信错误来自showSection()中的List.generate()。当generate()需要一个int,并且它得到Future时,会抛出一个异常。是的,但这不起作用。我确信错误来自showSection()中的List.generate()。当generate()需要一个int,并且它得到Future时,会抛出一个异常。是的,但这不起作用,但这并不能解决我的问题。首先,我不是通过传递快照来获取长度。我正在传递快照,以便获得填充Cereladata数组的对象列表。其次,我不能在非异步函数中使用wait。我还是试过你的方法,但没用。好吧,这解决了我的问题。谢谢但我没有得到一件事——我以为快照和文档是一样的。snapshots()返回什么?snapshot包含文档列表和metada信息,请检查抱歉,但这并不能解决我的问题。首先,我不是通过传递快照来获取长度。我正在传递快照,以便获得填充Cereladata数组的对象列表。其次,我不能在非异步函数中使用wait。我还是试过你的方法,但没用。好吧,这解决了我的问题。谢谢但我没有得到一件事——我以为快照和文档是一样的。快照()返回什么?快照包含文档列表和metada信息,请检查