I'm wondering if the given value is not null, but why is it giving an error null trying to enter ? or !
List<Cal>? KcalData; if changing from Cal to String
wrong in these two lines
KcalData.add(doc["foodName"]);
KcalData.add(doc["kCal"].toString());
List<Cal>? KcalData; If Cal will get another error which I don't know how to fix it.
KcalData?.add(doc["foodName"]);
KcalData?.add(doc["kCal"]);
Which one should be fixed and there is no error?
class Cal{
String? foodName;
double? kCal;
String? colorVal;
Cal(this.foodName,this.kCal,this.colorVal);
This section is fetching data from firebase.
Widget bulidChart(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection("chart").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return LinearProgressIndicator();
return SizedBox(height:200,child: ListView(children: getExpenseItems(snapshot)));
});
}
getExpenseItems(AsyncSnapshot<QuerySnapshot> snapshot) {
return snapshot.data!.docs
.map((doc) {
KcalData?.add(doc["foodName"]);
KcalData?.add(doc["kCal"].toString());
})
.toList();
}
}
Widget _buildChart(BuildContext context,List<Cal> KcalData ) {
List<charts.Series<Cal, String>>? datachart;
_generateData(KcalData) {
datachart!.add(
charts.Series(
id: 'Kcal',
data: KcalData,
labelAccessorFn: (Cal row, _) => "${row.foodName}",
domainFn: (Cal _cal, _) => _cal.foodName.toString(),
measureFn: (Cal _cal, _) => _cal.kCal,
colorFn: (Cal _cal, _) =>
charts.ColorUtil.fromDartColor(Color(int.parse(_cal.colorVal!)))),
);
}
_generateData(KcalData);
return Padding(
padding: EdgeInsets.all(10),
child: Container(
child: Center(
child: Column(
children: [
charts.PieChart(
datachart!,
animate: true,
behaviors: [
charts.DatumLegend(
entryTextStyle: charts.TextStyleSpec(
color: charts.MaterialPalette.purple.shadeDefault,
fontSize: 20,
fontFamily: 'Mitr'))
],
)
],
)),
),
);
}