Im new and my question may be stupid but
in class Location i have 2 var :
var latitude;
var longitude;
then :
Location({Key? key, this.latitude , this.longitude}) : super(key: key);
and after some works when I print them I get the value
print(widget.latitude);
print(widget.longitude);
there is no problem here BUT when I want to use these vars in another class like this :
var myLat = Location().latitude;
var myLong = Location().longitude;
the values are NULL
how can get the values the second class too?
CodePudding user response:
When you type Location()
- you create the new instance of this class. It isn't connected to the previous instance you were working on. To make this work you need to type
var myLat = location.latitude;
var myLong = location.longitude;
WHERE location
is the SAME object you created previously. So you need to pass it somehow to the place where you're trying to access these fields.
CodePudding user response:
as you know you have 2 ways:
- if you want to navigate from first page to second page you can pass the Location args by the code in my answer to a question. and if you want to read a doc here is the link.
- you can use Provider as a stateManagement. here is my answer to using provider. believe me that using stateManagement make your tasks easy .because sometimes you need to use datas from one page to 10 pages later. what you want to do?? do you want to pass datas to every pages????? if you didn't understand what i mean, please write in comment down. happy coding... Moraghebe khodet bash ;)