我可以解决因来自firestore的数据迟到而导致的错误

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

我正在使用圆形图表,其中int值来自firestorm。在圆形图表中,int值隐藏到double,但由于来自火灾存储的数据迟到,它显示我的错误(方法toDouble被调用null。接收器:null尝试调用:toDouble())一秒我使用下面的代码。

用于检索

@override
  void initState() {
    // TODO: implement initState
    super.initState();
    subscription = Firestore.instance.document("users/${widget.userid}").snapshots().listen((datasnapshot) {

      if (datasnapshot.data.containsKey(widget.alllist.title)) {
        setState(() {
          userscoren = datasnapshot.data[widget.alllist.title];
        });
      } else if (!datasnapshot.data.containsKey(widget.alllist.title)) {
        setState(() {
          userscoren = 0;
        });
      }


    });


}

在圆形图表中

 List<CircularStackEntry>_generateChartData(int stage){



    Color dialColor = Colors.green;
    labelcolor = dialColor;
    List<CircularStackEntry>data=[

      new CircularStackEntry([new CircularSegmentEntry(stage.toDouble(), dialColor,)])
    ];
    return data;
  }

在小部件中

@override
  Widget build(BuildContext context) {


    // TODO: implement build
        return  widget.intp==0?
        InkWell(
          child:  Card(
            child: Stack(
              children: <Widget>[
                Padding(padding: EdgeInsets.all(7.0),
                  child: new Container(
                    child: new AnimatedCircularChart(
                      key: _chartKey,
                      size: _chartSize,
                      initialChartData:_generateChartData(userscoren),
                      chartType: CircularChartType.Radial,
                      edgeStyle: SegmentEdgeStyle.round,
                      percentageValues: true,
                      holeRadius: 38.0,
                    ),
                    width: 80.0,
                    height: 80.0,
                    decoration: new BoxDecoration(
                      shape: BoxShape.circle,
                      border: Border.all(color: Colors.blue,width: 1.0,style: BorderStyle.solid,),
                      image: new DecorationImage(
                          fit: BoxFit.fill,
                          image: new NetworkImage(
                              widget.alllist.imageUrl)),
                    ),
                    margin: const EdgeInsets.symmetric(horizontal: 20.0),
//                      child: Text(name),
                  ),),


                Positioned(child:Center(
                    child: Text(widget.alllist.title,style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blueGrey),)),
                  bottom: 1.0,left: 20.0,right: 20.0,top: 85.0,
                ),
//          Padding(padding: EdgeInsets.only(top: 80.0,left: 10.0),
//            child: Text(alllist.title,style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blueGrey),),
//          ),
              ],
            ),
          ),
          onTap: (){
            if(widget.alllist.topictype =="Description&Question"){
              Navigator.of(context).push(new MaterialPageRoute(builder: (context)=>new DisAndQuesShow(alllist: widget.alllist,userid: widget.userid,)));

            }else{
              if(widget.alllist.topictype=="Description"){

                Navigator.of(context).push(new MaterialPageRoute(builder: (context)=>new Discribtion(alllist: widget.alllist,)));
              }

            }
          },
        )
flutter google-cloud-firestore
2个回答
1
投票

首先避免在主线程上使用Futures调用网络调用。对于你的问题,试试这个。

var userscoren = 0;

Future<Null> checkUserData() async{
subscription = Firestore.instance.document("users/${widget.userid}").snapshots().listen((datasnapshot) {

  if (datasnapshot.data.containsKey(widget.alllist.title)) {
    setState(() {
      userscoren = datasnapshot.data[widget.alllist.title];
    });
  } else if (!datasnapshot.data.containsKey(widget.alllist.title)) {
    setState(() {
      userscoren = 0;
    });
  }
});
}

@override
void initState() {
// TODO: implement initState
super.initState();
checkUserData();
}

正如@ F-1所提到的,你也可以在FutureBuilder中使用以上的功能。


0
投票

您应该尝试FutureBuilder类,以便在调用toDouble方法https://docs.flutter.io/flutter/widgets/FutureBuilder-class.html时等待数据,从而避免返回null

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