I am trying to get lists of averages rating from JSON rest API in Flutter. When there is rating value for all product the listview displays fine but when any product has an empty rating the listview returns an error "NoSuchMethodError: The getter 'length' was called on null. Receiver: null. Tried calling: length. Below is my code;
ListView.separated(
separatorBuilder: (context, index) =>
Divider(
color: Colors.grey,
),
padding: const EdgeInsets.all(5.0),
itemCount: content.length,
itemBuilder: (context, position) {
final current = content[position];
double myrate = double.parse(content[position].ratings);
return Container(
child: SmoothStarRating(
allowHalfRating: true,
onRatingChanged: (v) {
setState(() {});
},
starCount: 5,
rating: myrate,
halfFilledIconData: Icons.star_half,
size: 20.0,
filledIconData: Icons.star,
color: Colors.orange,
borderColor: Colors.orange,
spacing: 0.0)
)
})
So I was able to solve this error by simply adding returning "0" when my rating is empty and the actual value when it is not empty. With this line of code:
double myrate = double.parse( content[position].ratings==null ? "0" : content[position].ratings);