I have created a map named starsMap in the class[step 1]. I have not initialized any value to it. after that, I am calling a function then assigning response data to value to it [step 2], and after that assigning data to a1Rating [step 3]. but I am getting the following error.
class _GiveFeedbackScreenState extends State<GiveFeedbackScreen> {
// Getting feedback data | stars
// step : 1
Map starsMap = {};
getFeedbackStars() async {
final prefss = await SharedPreferences.getInstance();
var token = prefss.getString('token');
var subject_id = widget.subjectId;
var feedback_id = widget.feedbackId;
var response = await http.get(
Uri.parse(
"https://xxxxxxx"),
headers: {
'Authorization': 'Bearer $token',
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
);
var data = jsonDecode(response.body)['result'];
// step : 2
this.starsMap = {
"a1": data['a1'],
"a1": data['a2'],
"a1": data['b1'],
"a1": data['b2'],
};
// print(this.starsMap);
return jsonDecode(response.body);
}
// [step : 3 ]
double a1Rating = this.starsMap['a1']; // error here
//The instance member 'starsMap' can't be accessed in an initializer.
//Try replacing the reference to the instance member with a different expression
double a2Rating = this.starsMap['a2'];
double b1Rating = this.starsMap['b1'];
double b2Rating = this.starsMap['b2'];
and I have to show that rating into RatingBar
RatingBar(
initialRating: this.a1Rating!,
direction: Axis.horizontal,
allowHalfRating: false,
itemCount: 5,
ratingWidget: RatingWidget(
full: const Icon(Icons.star,
color: Colors.orange),
half: const Icon(
Icons.star_half,
color: Colors.orange,
),
empty: const Icon(
Icons.star_outline,
color: Colors.orange,
)),
onRatingUpdate: (value) {
a1Rating = value;
// setState(() {});
}),