I have a few pages that need the user location for calculations but the issue is everytime on these pages Im using the following method everytime I load the screens.
void getCurrentLocation() async {
Location _location = Location();
await _location.getLocation().then(
(newLocation) {
setState(() {
currentLocation = newLocation;
});
},
);
}
Is there a way I can run this run once on startup or have it running in the background and access it easily in every page to save on load times? as my app has to load every screen and feels very slow
CodePudding user response:
You can always wrap the whole app in blocProvider and have a bloc for collecting location and storing it eg: User model that has location as a parameter. Then information is collected only once (if you tell it to) and will be accessible throughout the whole app from context.
Of course there is a quick and messy solution. Create file location.dart or whatever, create parameter const String location;
or however else you want to store it (List<double> [lat lon]
). Make a loading screen at the start that collects user location and assign it to before-mentioned variable. Then import 'whatever/location.dart' as location;
to every widget that uses location and access it with location.location
CodePudding user response:
Try creating a global variable for Location.
Now, on app init store the current Location in a global variable. & you can use it anywhere.