Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: The method 'getPositionStream' isn't defined for the class 'Geolocator'

Using flutter I try to retrieve the current position using StreamSubscription and getPositionStream in geo locator. But I keep getting the error: Error: The method 'getPostionStream' isn't defined for the class 'Geolocator'.

Here is my function:

void getLocationUpdates(){
StreamSubscription<Position> homeTabPostionStream;

homeTabPostionStream = geoLocator.getPositionStream(locationOptions).listen((Position position){
  currentPosition = position;
});

Here I created the variables:

var geoLocator = Geolocator();
var locationOptions = LocationOptions(accuracy: LocationAccuracy.bestForNavigation, distanceFilter: 4);

As you requested I write the entire code for reference.

Here is the entire code:

class HomeTab extends StatefulWidget {
  @override
  _HomeTabState createState() => _HomeTabState();
}

class _HomeTabState extends State<HomeTab> {

  GoogleMapController mapController;
  Completer<GoogleMapController> _controller = Completer();

  Position currentPosition;

  DatabaseReference tripRequestRef;

  var geoLocator = Geolocator();
  var locationOptions = LocationOptions(accuracy: LocationAccuracy.bestForNavigation, distanceFilter: 4);


  void getCurrentPosition() async{

    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.bestForNavigation);
    currentPosition = position;
    LatLng pos = LatLng(position.latitude, position.longitude);
    mapController.animateCamera(CameraUpdate.newLatLng(pos));

  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        GoogleMap(
          padding: EdgeInsets.only(top: 135),
          myLocationEnabled: true,
          myLocationButtonEnabled: true,
          mapType: MapType.normal,
          initialCameraPosition: googlePlex,
          onMapCreated: (GoogleMapController controller){
            _controller.complete(controller);
            mapController = controller;

            getCurrentPosition();
          },
        ),
        Container(
          height: 135,
          width: double.infinity,
          color: BrandColors.colorPrimary,
        ),

        Positioned(
          top: 60,
          left: 0,
          right: 0,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              AvailabilityButton(
                title: 'GO ONLINE',
                color: BrandColors.colorOrange,
                onPressed: (){

                  GoOnline();

                },
              ),
            ],
          ),
        ),

      ],
    );
  }

  void GoOnline(){
    Geofire.initialize('driversAvailable');
    Geofire.setLocation(currentFirebaseUser.uid, currentPosition.latitude, currentPosition.longitude);

    tripRequestRef = FirebaseDatabase.instance.reference().child('drivers/${currentFirebaseUser.uid}/newtrip');
    tripRequestRef.set('waiting');
    
    tripRequestRef.onValue.listen((event) {



    });

  }

  void getLocationUpdates(){
    StreamSubscription<Position> homeTabPostionStream;

    homeTabPostionStream = geoLocator.getPositionStream(locationOptions).listen((Position position){
      currentPosition = position;
    });
like image 665
maximus383 Avatar asked Nov 12 '20 19:11

maximus383


1 Answers

No need to instantiate Geolocator. getPositionStream() is a static method called like so, Geolocator.getPositionStream(). The args passed to the method are all optional params as well. No locationOptions necessary.

Updated

void getLocationUpdates(){
  StreamSubscription<Position> homeTabPostionStream;

  homeTabPostionStream = Geolocator.getPositionStream(
    desiredAccuracy: LocationAccuracy.bestForNavigation,
    distanceFilter: 4).listen((event) {
       ...
    });
}

You can remove

var geoLocator = Geolocator();
var locationOptions = LocationOptions(accuracy: LocationAccuracy.bestForNavigation, distanceFilter: 4);
like image 152
Lee3 Avatar answered Sep 21 '22 00:09

Lee3