In the following code, the output is coming out to be null, not 1. Can someone explain it to me?

Issue

I declared one variable in dart as follows-

var Latitude;

void GetCLocation() async {
  Latitude=1;
}

main(){
   print(Latitude);
}

The problem is that the output is not coming out to be 1, but it is coming out to be null. What am I doing wrong?

Solution

Do this instead, you should call the function for it to be assign:

void main() {
  
  var Latitude;
  
  void GetCLocation() async {
  Latitude=1;
  
  }
  
  GetCLocation();
  
  print(Latitude);
  
  
}

Answered By – Wilson Toribio

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *