将速度转换为 mph(Flutter)

Convert speed to mph (Flutter)

我在 Firestore 中的默认速度记录为每秒米数,我需要每小时英里数。我知道我可以乘以 2.23694,这将是等效的转换,但我不知道如何将其添加到我的代码中。如何将速度从米每秒更改为英里每小时?感谢阅读和任何建议。

import 'dart:async';
import 'dart:developer';
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';

Future<void> backgroundUpdate() async {

  Firebase.initializeApp();
  FirebaseAuth auth = FirebaseAuth.instance;
  Position _fetchedUserLocation = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  log(_fetchedUserLocation.latitude.toString() +
      _fetchedUserLocation.longitude.toString());

  User firebaseUser = (auth.currentUser)!;
  String uid = firebaseUser.uid;
  String displayNamed = firebaseUser.displayName!;
  DateTime dateTime = DateTime.now();
  String dateString = DateFormat('dd-MM-yyyy - kk:mm').format(dateTime);
  double latitude = _fetchedUserLocation.latitude.toDouble();
  double longitude = _fetchedUserLocation.longitude.toDouble();
  String speed = _fetchedUserLocation.speed.toString();

  Map<String, dynamic> map = Map();
  map['DateTime'] = dateString;
  map['Uid'] = uid;
  map['Racer'] = displayNamed;
  map['Lat'] = latitude;
  map['Lng'] = longitude;
  map['Speed'] = speed;

  Map<String, dynamic> map2 = Map();
  map2['DateTime'] = dateString;
  map2['Racer'] = displayNamed;
  map2['Lat'] = latitude;
  map2['Lng'] = longitude;
  map2['Speed'] = speed;
  map2['Uid'] = uid;

  FirebaseFirestore firestore = FirebaseFirestore.instance;
  CollectionReference collectionReference =
  firestore.collection('$displayNamed');
  await collectionReference.doc('$dateString').set(map).then((
      value) {
    print('Upload Success Dawg');
  });

  FirebaseFirestore firestore2 = FirebaseFirestore.instance;
  CollectionReference collectionReference2 =
  firestore2.collection('usertest');
  await collectionReference2.doc('$displayNamed').set(map2).then((
      value) {
    print('Upload Success Dawgydeuce');
  });
}

改变这个:

String speed = _fetchedUserLocation.speed.toString();

进入这个:

double speedInMetersPerSecond = _fetchedUserLocation.speed;
double speedInMilesPerHour = speedInMetersPerSecond * 2.23694;

或更紧凑一点:

final speedInMilesPerHour = _fetchedUserLocation.speed * 2.23694;

然后根据需要使用speedInMilesPerHour

我先把speed的值改成double然后转成mph然后改回string

import 'dart:async';
import 'dart:developer';
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';

Future<void> backgroundUpdate() async {

  Firebase.initializeApp();
  FirebaseAuth auth = FirebaseAuth.instance;
  Position _fetchedUserLocation = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  log(_fetchedUserLocation.latitude.toString() +
      _fetchedUserLocation.longitude.toString());

  User firebaseUser = (auth.currentUser)!;
  String uid = firebaseUser.uid;
  String displayNamed = firebaseUser.displayName!;
  DateTime dateTime = DateTime.now();
  String dateString = DateFormat('dd-MM-yyyy - kk:mm').format(dateTime);
  double latitude = _fetchedUserLocation.latitude.toDouble();
  double longitude = _fetchedUserLocation.longitude.toDouble();
  String speed = _fetchedUserLocation.speed.toString();
  final double speeddouble = double.parse(Speed) * 2.23694;
  final String speedfinal = speeddouble.toString();
  


  Map<String, dynamic> map = Map();
  map['DateTime'] = dateString;
  map['Uid'] = uid;
  map['Racer'] = displayNamed;
  map['Lat'] = latitude;
  map['Lng'] = longitude;
  map['Speed'] = speed;

  Map<String, dynamic> map2 = Map();
  map2['DateTime'] = dateString;
  map2['Racer'] = displayNamed;
  map2['Lat'] = latitude;
  map2['Lng'] = longitude;
  map2['Speed'] = speedfinal;
  map2['Uid'] = uid;

  FirebaseFirestore firestore = FirebaseFirestore.instance;
  CollectionReference collectionReference =
  firestore.collection('$displayNamed');
  await collectionReference.doc('$dateString').set(map).then((
      value) {
    print('Upload Success Dawg');
  });

  FirebaseFirestore firestore2 = FirebaseFirestore.instance;
  CollectionReference collectionReference2 =
  firestore2.collection('usertest');
  await collectionReference2.doc('$displayNamed').set(map2).then((
      value) {
    print('Upload Success Dawgydeuce');
  });
}