0

` class Location{

     late double lat;
     late  double lon;
    
      void  getLocation( ) async{
        Position position = await _determinePosition();
        lat=position.latitude;
        lon=position.longitude;
    
      }
    
    
    
    
    
      Future<Position> _determinePosition() async {
    
        LocationPermission permission;
        permission=await Geolocator.checkPermission();
        if(permission==LocationPermission.denied){
          permission=await Geolocator.requestPermission();
          if(permission==LocationPermission.denied){
            return Future.error('Permission Denied');
          }
        }
        return await Geolocator.getCurrentPosition();
    
    
      }
    
    
    }

`How to initialize the value of varibale lat and lon in this? , i have done everything in my reach, but it always ends with error 'Non-nullable instance field 'lon' must be initialized. (Documentation) Try adding an initializer expression, or a generative constructor that initializes it, or mark it 'late'. and if i add late than it show runtime error on my device,that late variable should be initialized.

Vipin
  • 21
  • 2

1 Answers1

1
 double? lat;
  double? lng;  

///get location
 void getLocation(pr) async {
  await [
      Permission.location,
    ].request();
    if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
      print('no');
    }
   Position position = await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.high);
    lat = position.latitude;
    lng = position.longitude;
}
Osama Kashif
  • 366
  • 1
  • 5