【发布时间】:2021-04-07 12:38:08
【问题描述】:
我拼凑了来自 github 的 this 的变体。它结合了来自 firebase 的用户 ID 和 location 插件。基本上它的设置是为了让一个按钮触发一个firestore插入。我想要做的是将位置流式传输到数据库,而不是只发布一次。如何修改我的代码以实现这一点?具体来说,“Map
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:location/location.dart';
import 'package:location_platform_interface/location_platform_interface.dart';
class AddLocation extends StatefulWidget {
final double lat;
final double lng;
final double speed;
AddLocation({Key key, this.lat, this.lng, this.speed}) : super(key: key);
@override
_AddLocationState createState() => _AddLocationState();
}
class _AddLocationState extends State<AddLocation> {
// Field
double lat, lng;
double speed;
String dateString;
// Method
@override
void initState() {
super.initState();
// findLatLng();
setState(() {
lat = widget.lat;
lng = widget.lng;
speed = widget.speed;
print('latlng on AddLocaatinn ===>> $lat, $lng, $speed');
});
}
Future<Stream<void>> findLatLng() async {
LocationData locationData = findLocationData() as LocationData;
setState(() {
lat = locationData.latitude;
lng = locationData.longitude;
speed = locationData.speed;
print('lat, lng on add Locaton ===>>>$lat, $lng, $speed');
});
}
Future<Stream<LocationData>> findLocationData() async {
var location = Location();
try {
return location.onLocationChanged;
} catch (e) {
print('e AddLocation ==>> ${e.toString()}');
return null;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
margin: const EdgeInsets.only(right: 42),
child: RaisedButton(
child: const Text('Post User Location'),
onPressed: insertDataToFirestore,
),
),
],
),
),
);
}
Future<Stream<Null>> insertDataToFirestore() async {
FirebaseAuth auth = FirebaseAuth.instance;
FirebaseUser firebaseUser = await auth.currentUser();
String email = firebaseUser.email;
String uid = firebaseUser.uid;
String photoURL = firebaseUser.photoUrl;
String displayName = firebaseUser.displayName;
DateTime dateTime = DateTime.now();
String dateString = DateFormat('dd-MM-yyyy - kk:mm').format(dateTime);
Map<String, dynamic> map = Map();
map['DateTime'] = dateString;
map['Lat'] = lat;
map['Lng'] = lng;
map['Speed'] = speed;
map['Email'] = email;
map['Uid'] = uid;
map['PhotoURL'] = photoURL;
map['DisplayName'] = displayName;
Firestore firestore = Firestore.instance;
CollectionReference collectionReference =
firestore.collection('location');
await collectionReference.document().setData(map).then((value) {
print('Upload Success');
});
}
}
【问题讨论】:
标签: firebase flutter dart google-cloud-firestore