Search code examples
flutterdartfl-chart

Unable to fill spots array in fl_charts


I am working with fl_charts in flutter and can't seem to fill in the spots array with continuous data. My data is continuously coming from Firebase's Realtime Database and I tried to set the state of an array equal to elements of type FlSpot as seen below.

var temp_arr = [];
setState(() { 
  for(int i = 0; i<tempList.length; i++) {
      temp_arr[i] = FlSpot((i*1.0), double.parse(tempList[i].humidity))
    }

});

Then when I tried to do this spots:temp_arr

final LineChartBarData lineChartBarData2 = LineChartBarData(
  
    spots: temp_arr
...

I would get an error saying it can't find the range. Do you think I am close or is there a better way to fill our graph with dynamic data?


Solution

  • I found out how to feed live data. And am currently working on how to fix the range.

    List<FlSpot> gatherWeightData(){
        List<FlSpot> spotWeightList = [];
        List<FlSpot> removedSpotWeightList = [];
        for (int i = 0; i < weightList.length; i++) {
          spotWeightList.add(FlSpot((i * 1.0), double.parse(weightList[i].hive_weight)));
          if (spotWeightList.length >= 19) {
            spotWeightList.removeAt(0);
            removedSpotWeightList = spotWeightList;
            return removedSpotWeightList;
          }
        }
        return spotWeightList;
      }
    

    Here we are creating a List of type FlSpot and adding in (x,y) values needed, then I am having spots equal to the method that is creating these lists.

    List<LineChartBarData> linesBarData1() {
        LineChartBarData lineChartBarData1 = LineChartBarData(
          spots: gatherHumidityData(),
    ...