Using getPositionStream (GeoLocator)

Custom Code

Hi,

I am working through changing from getCurrentLocation to getPositionStream. As I need continuous navigation it is the recommended solution as far as I can see. The code below compiles and works.

My issue is that this gives me a single location, even though I have opened a position stream, which will continue feeding results.

Does anyone know if I re-run the same custom action if this opens another position stream, or reuses the one that's already open? That is what I will try next, and just see how it impacts performance, but there are so many people here that I´m guessing someone knows 🙂

After I exit my navigation phase I know I need to close down the stream, but I haven´t made this custom action as yet.



What have you tried so far?
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/actions/actions.dart' as action_blocks;
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:geolocator/geolocator.dart';
import 'package:geolocator_apple/geolocator_apple.dart';
import 'package:geolocator_android/geolocator_android.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:universal_io/io.dart';

Future<List<double>> streamStart() async {
  LocationSettings locationSettings = LocationSettings(
    accuracy: LocationAccuracy.high,
    distanceFilter: 100,
    timeLimit: Duration(seconds: 10),
  );

  List<double> coordinates = [];

  if (Platform.isAndroid) {
    LocationSettings locationSettings = AndroidSettings(
      accuracy: LocationAccuracy.high,
      distanceFilter: 2,
      forceLocationManager: true,
    );
  } else if (Platform.isIOS) {
    LocationSettings locationSettings = AppleSettings(
      accuracy: LocationAccuracy.best,
      distanceFilter: 2,
      activityType: ActivityType.otherNavigation,
    );
  }

  StreamSubscription _positionStream = Geolocator.getPositionStream(
    locationSettings: locationSettings,
  ).listen(
    (Position position) {
      coordinates.add(position.latitude);
      coordinates.add(position.longitude);
    },
  );

  // double latitude = position.latitude;
  // double longitude = position.longitude;
  // Return the latitude and longitude as a list of doubles
  return coordinates;
}
Did you check FlutterFlow's Documentation for this topic?
Yes