1
votes

I'm new to Flutter and Firestore and feel I am missing something basic in how to handle doubles and Firebase. I have Flutter code reading and writing Strings and int to Firestore and all is well. When I set up a Firestore field as number life get complicated. If in flutter I define a variable as num (and ignore the Flutter double data type) all is well. If I define the Flutter variable as double nothing works, unless I ensure the Firestore number field has say 10.2 (or whatever value with a decimal point). A value of 10 will crash the app!

  1. Should I just always use num in Flutter and not use double? Any implications?

  2. With the code reading from Firestore ( propertyLandArea: snapshot.data['propertyLandArea'] ?? 10.5,) should I do checks or somehow ensure properyLandArea is assigned a double. Using something simple like snapshot.data['propertyLandArea'].toDouble is not possible.

  3. Should I just use int, and know the number of decimal places required so can display and save correctly (if I don't forget to convert somewhere).

1

1 Answers

1
votes

I faced this problem a couple of times. I used casting to convert the number to double when I was receiving the data and when I was saving i used double and firebase converted to number.

1) I personally wouldn't use number because if you want to leave from firebase you must convert your model. I don't think that you are going to have any other implications.

2) I don't think it's necessary to check. If you want to be sure that you are receiving a double you can use validation on the security rules.

3) No. As you said, you can easily forget it to change it somewhere, your problem is easily fixable, and you never know where you can use that extra information.