Reputation: 156
I am trying to write a value from the list named "data" to firestore, the list has two elements with two values, mileage and geodistance. Currently firestore is writing both the mileage and the geodistance to firestore when in reality I only need the mileage written to firestore. Currently I'm using final mileager = data.elementAt(0); to get the element into firestore but it's uploading everything in the element when I want only a single value from the element. How do I get just a single value from this list and write it to firestore? Basically I am using geolocator to see which point is closest, then I want to write the "mileage" of that point to firestore. The photo below shows how the mileage is currently being written. This is close but I want it as only a single field and value, not as a map.
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:geolocator/geolocator.dart';
import 'package:intl/intl.dart';
final _firestore = FirebaseFirestore.instance;
class Geodistancer {
final int? mileage;
final double geodistance;
const Geodistancer({
required this.mileage,
required this.geodistance,
});
}
Future<Position> getLocation() async {
var currentLocation;
try {
currentLocation = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best);
} catch (e) {
currentLocation = null;
}
return currentLocation;
}
Future<void> backgroundUpdate() async {
Firebase.initializeApp();
FirebaseAuth auth = FirebaseAuth.instance;
Position _fetchedUserLocation = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
getLocation().then((position) async {
_fetchedUserLocation = position;
final distanceInMiles1 = Geolocator.distanceBetween(
_fetchedUserLocation.latitude, _fetchedUserLocation.longitude,
45.4906999, -122.49727);
final distanceInMiles2 = Geolocator.distanceBetween(
_fetchedUserLocation.latitude, _fetchedUserLocation.longitude, 45.48144,
-122.50433);
List<dynamic> data = [
{"mileage": 1, "geodistance": distanceInMiles1},
{"mileage": 2, "geodistance": distanceInMiles2},
];
data.sort((a, b) => a["geodistance"].compareTo(b["geodistance"]));
User firebaseUser = (auth.currentUser)!;
String uid = firebaseUser.uid;
DateTime dateTime = DateTime.now();
String dateString = DateFormat().format(dateTime);
double latitude = _fetchedUserLocation.latitude.toDouble();
double longitude = _fetchedUserLocation.longitude.toDouble();
final speedInMilesPerHour = _fetchedUserLocation.speed * 2.23694;
final mileager = data.elementAt(0);
_firestore
.collection('locations')
.add({
'Uid': uid,
'DateTime': dateString,
'Mileage': mileager,
'Speed': speedInMilesPerHour.round().toString() + ' MPH',
'position': GeoPoint(latitude, longitude)
});
});
}
Upvotes: 3
Views: 147
Reputation: 12353
Change it to this:
final mileager = data.elementAt(0)["mileage"];
Because previously, you were getting the element at index 0 properly, but this element is a Map. What you want, is the value that is at the key "mileage" in this element which is at index 0. This element should be posted to firebase as a single field,not a map, and you should be good.
Keep the rest of your code as it is.
Upvotes: 3