【问题标题】:How to get a single value from a list element如何从列表元素中获取单个值
【发布时间】:2021-08-03 15:59:06
【问题描述】:

我正在尝试将名为“data”的列表中的一个值写入 firestore,该列表有两个元素,其中包含两个值,即里程和地理距离。目前firestore正在写入里程和地理距离到firestore,而实际上我只需要写入firestore的里程。目前我正在使用 final mileager = data.elementAt(0);将元素放入firestore,但是当我只需要元素中的一个值时,它会上传元素中的所有内容。如何从该列表中仅获取一个值并将其写入 Firestore?基本上我正在使用地理定位器来查看哪个点最近,然后我想将该点的“里程”写入firestore。下面的照片显示了里程当前是如何写入的。这很接近,但我希望它只是一个字段和值,而不是地图。

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)
    });
  });
}

【问题讨论】:

  • 你只需要里程数作为字符串吗?
  • 正确,收到以下解决方案,但谢谢

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

改成这样:

final mileager = data.elementAt(0)["mileage"];

因为之前,您正确获取了索引 0 处的元素,但该元素是一个 Map。您想要的是该元素中键“里程”处的值,该元素位于索引 0 处。该元素应作为单个字段而不是地图发布到 Firebase,您应该很好。

保持其余代码不变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-28
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多