Home > Software engineering >  Why i am not getting the actual latutude and longitude? Latitude: Instance of 'LOCATION'.l
Why i am not getting the actual latutude and longitude? Latitude: Instance of 'LOCATION'.l

Time:02-27

why i am getting instance of latitude and instance of longitude while trying to get the actual latitude and longitude. Here i am using the geolocator package of flutter. Please someone help me your efforts will be appreciated.

location.dart

import 'package:geolocator/geolocator.dart';

class LOCATION {
  double latitued = 0.0;
  double longitude = 0.0;
  Future<void> getCurrentLocation() async {
    try {
      Position currentPosition;
      currentPosition = await Geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.low);
      latitued = currentPosition.latitude;
      longitude = currentPosition.longitude;
    } catch (e) {
      print(e);
    }
  }
}

loading_screen.dart

import 'package:flutter/material.dart';
import 'package:clima_v1/services/location.dart';
import 'package:http/http.dart';

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {
  void getLocation_v1() async {
    LOCATION location = LOCATION();
    await location.getCurrentLocation();
    print('Latitude: $location.latitude and Longitude: $location.longitude');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            //Get the current location
            getLocation_v1();
          },
          child: Text('Get Location'),
        ),
      ),
    );
  }
}

Output:

I/flutter ( 9688): Latitude: Instance of 'LOCATION'.latitude and Longitude: Instance of 'LOCATION'.longitude

CodePudding user response:

You should use curly braces:

print('Latitude: ${location.latitude} and Longitude: ${location.longitude}');

Otherwise, you're just printing location, not location.latitude.

  • Related