【问题标题】:Flutter Stream Location and User Data to firestoreFlutter 流位置和用户数据到 Firestore
【发布时间】:2021-04-07 12:38:08
【问题描述】:

我拼凑了来自 github 的 this 的变体。它结合了来自 firebase 的用户 ID 和 location 插件。基本上它的设置是为了让一个按钮触发一个firestore插入。我想要做的是将位置流式传输到数据库,而不是只发布一次。如何修改我的代码以实现这一点?具体来说,“Map map = Map();”下立即列出的所有内容是我想要流式传输到 Firestore 的内容。感谢阅读。

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


    【解决方案1】:

    你的想法是跳出框框

    Location location = new Location();
    

    这里的位置对象有一个流是onLocationChanged()

    您收听这样的流,该事件将包含有关位置所需的数据

    initState(){
      Location location = new Location();
      location.onLocationChanged().listen((event){
      //this function gets called every time the location changes
      //so you just have to insert it database/firestore here
      //effectively making it like the way you want
      //update your lat lang
      insertDataToFirestore();//this function has not to be a stream
     });
    }
    
    
      Future 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');
        });
      }
    

    【讨论】:

    • 您好,感谢您的回复。我不太了解我需要在哪里更改我的代码。如果您能提供一些额外的见解,我们将不胜感激。
    • 你需要了解流,先去here
    • 谢谢,我还是不太明白,但我会读一些关于流的内容。不幸的是,该链接适用于付费订阅,但我仍会查看文档并四处搜索。
    • 是的,阅读有关流的文章,忽略那篇媒体文章
    • 非常感谢你让我用你的代码指出了正确的方向。我最终让它与这个一起工作。 'code' initState(){ Location location = new Location(); location.onLocationChanged.listen((res) { setState(() { lat = res.latitude; lng = res.longitude; }); insertDataToFirestore(); }); }
    猜你喜欢
    • 2020-10-20
    • 2020-07-08
    • 2021-08-27
    • 2020-09-12
    • 2020-03-01
    • 2020-08-02
    • 2020-03-22
    • 2021-04-20
    • 1970-01-01
    相关资源
    最近更新 更多