how to assign values to global variables in a function and use them in other functions in dart

128

Solution 1

try below code hope its help to you. just call your varcheck() inside main main method

double? latitude;
double? longitude;
void varcheck() {
  latitude = 33;
  longitude = 44;
}

void main() {
  varcheck();
  print(latitude);
  print(longitude);
}

Your result-> 33 44

Solution 2

You did not call varcheck() function .So you have to call this function first.

double? latitude;
double? longitude;
void varcheck(){
  latitude=33;
  longitude=44;
}
void main() {
  varcheck();
  print(latitude);
  print(longitude);
}
Share:
128
Rasool Zada
Author by

Rasool Zada

Updated on January 02, 2023

Comments

  • Rasool Zada
    Rasool Zada over 1 year

    I tried to declare variable in dart in this way but it shows a warning due to null safety.

    double latitude; 
    double longitude;
    

    Again I tried like this but the values did not assign to the variables latitude and longitude in the function varcheck().

    double? latitude;
    double? longitude;
    void varcheck(){
      latitude=33;
      longitude=44;
    }
    void main() {
      print(latitude);
      print(longitude);
    }
    

    Output null null

    how to assign the values to the global variables in a function and use them in any other functions