Jake
Jake

Reputation: 2056

Dart - Converting num to double

Using a Flutter package FlutterRatingBarIndicator, I need to use a double to determine a x/5 rating. Using another package, flutter_google_places to be specific, I get the num value f.rating.

With f.rating, I should be able to convert from num to double using something like f.rating.toDouble() but this is returning null.

I'm not sure why it's returning null as f.rating.toString() returns a non-null value such as 3.5

Is there any special casting I need to apply before a num can be used as a double value?

Thanks

Edit; How I get f.rating:

Using flutter_google_places I receive rating as a num value from result :

  void getNearbyPlaces(LatLng center) async {
    final location = Location(lat, lng);
    final result = await _places.searchNearbyWithRadius(location, 2500, type: 'restaurant');

    setState(() {
      if (result.status == "OK") {
        this.places = result.results;
      }
      print(result.status);
      print(places.toString());
    });
  }

This later appears as places.map :

  final placesWidget = places.map((f) {}).toList();

Finally this is used in a flutter_rating_bar :

  FlutterRatingBarIndicator(
    rating: f.rating,
    itemCount: 5,
    itemSize: 15.0,
    emptyColor: Colors.amber.withAlpha(100),
    itemPadding: EdgeInsets.only(top: 2.5, bottom: 6),
  ),

Upvotes: 0

Views: 2031

Answers (2)

Jake
Jake

Reputation: 2056

This isn't the best way around this error, but as far as I've learned a num should be supported by double specifying parameters. I can't put a pin in the cause of this error as I didn't write/don't totally understand the google_maps_flutter package.

The solution I'll be using looks as follows...

double drating = 0.0;

final placesWidget = places.map((f) {
   ...
   drating = f.rating.toDouble();
   ...
}).toList();

I can then use drating as such...

FlutterRatingBarIndicator(
   rating: drating,
   itemCount: 5,
   itemSize: 15.0,
   emptyColor: Colors.amber.withAlpha(100),
   itemPadding: EdgeInsets.only(top: 2.5, bottom: 6),
),

Upvotes: 1

Mo Ok
Mo Ok

Reputation: 544

num datatype in dart can store double or integers values,

you don't have to convert it to double!!

Upvotes: 1

Related Questions