when i add late to " List<charts.Series<Sales,String>> _seriesBarData;"
it show me this error :
LateInitializationError: Field '_seriesBarData@905436988' has not been initialized. and then when i delete it and add '?'
it show me this error
Error: The argument type 'List<Series<Sales, String>>?' can't be assigned to the parameter type 'List<Series<dynamic, String>>' because 'List<Series<Sales, String>>?' is nullable and 'List<Series<dynamic, String>>' isn't.
here is my code
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'Model/sales.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'rounded_button.dart';
class ChartScreen extends StatefulWidget {
@override
_ChartScreenState createState() => _ChartScreenState();
}
class _ChartScreenState extends State<ChartScreen> {
late List<charts.Series<Sales,String>> _seriesBarData;
List<Sales>? myData ;
_generateData(myData){
_seriesBarData = <charts.Series<Sales,String>>[];
_seriesBarData?.add(
charts.Series(
domainFn : (Sales sales,_) => sales.saleYear.toString() ,
measureFn : (Sales sales,_) => sales.saleVal,
colorFn : (Sales sales,_) => charts.ColorUtil.fromDartColor(Color(int.parse(sales.colorVal))),
id:"Sales",
data: myData ,
labelAccessorFn: (Sales row,_) => "${row.saleYear}"
)
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: _buildBody(context),
);
}
Widget _buildBody(BuildContext context){
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('sales').snapshots(),
builder: (context,snapshot){
if(!snapshot.hasData){
return LinearProgressIndicator();
}
else{
List<Sales> sales = snapshot.data!.docs
.map((snapshot) => Sales.fromMap(snapshot.data() as Map<String,dynamic>))
.toList();
return _buildChart(context, sales);
}
},
);
}
Widget _buildChart(BuildContext context , List<Sales> saledata){
List<Sales> myData ;
myData = saledata;
_generateData(myData){};
return Padding(
padding: EdgeInsets.all(8.0),
child: Container(
child: Center(
child:Column(
children: <Widget>[
Text ('Sales by Year',
style:TextStyle(fontSize:24.0 , fontWeight: FontWeight.bold),
),
SizedBox(height : 10.0, ),
Expanded(
child: charts.BarChart(_seriesBarData,
/*animate : true,
animationDuration: Duration(seconds:5),*/
behaviors : [
new charts.DatumLegend(
entryTextStyle : charts.TextStyleSpec(color: charts.MaterialPalette.purple.shadeDefault,
fontFamily: 'Google',
fontSize:18),
)
],
),
),
],
),
),
),
);
}
}
CodePudding user response:
Maybe it was a mistake while copying your code to SO, but looking at your call to _generateData
, it seems to be wrong:
_generateData(myData){};
This should simply be
_generateData(myData);
In this case, it makes sense that you get the error, since further down, you call
charts.BarChart(
_seriesBarData,
...
)
And if BarChart
gets null (your initial value), your program will crash.
I agree with the other commenter that you can initialize your array at its declaration, that will make sure no crash occurs (though unless you fix the function call, the list will remain empty).
List<charts.Series<Sales, String>> _seriesBarData = [];
CodePudding user response:
Firstly since the parameter "myData" is nullable then you need to write it like this myData!
also delete the word late late List<charts.Series<Sales,String>> _seriesBarData;
also make it like List<charts.Series<Sales,String>> _seriesBarData = <charts.Series<Sales,String>>[];
so I think your problem will be solved.