-2
void getLocation() async {
  await location.getCurrentLocation();
  *latlng* = LatLng(location.latitude, location.longitude);}

. .

 @override
 Widget build(BuildContext context) {
  return Scaffold(
   body: Stack(
     children: [
       GoogleMap(
         onMapCreated: _onMapCreated,
         mapType: MapType.normal,
         initialCameraPosition: CameraPosition(
           target: *latlng*,
           zoom: 14.4746,
         ),
       ),

I want to use latlng in widget. However, 'target == null' is output asynchronously. What should I do?

  • Welcome to Stackoverflow. If you are getting no answers this is usually due to the fact that you have not asked in a very clear and detailed way. Please add more information then you will have a better impact. –  Dec 18 '20 at 04:06
  • Use if else, check if latlng is null then show loader until latlng get after latlng receive pass it to map – Shubham Narkhede Dec 18 '20 at 04:34

1 Answers1

0

usually what we do is declare a Boolean for example coordAvbl = flase;

and on running the async function of getting the location you use a setState to change to the boolean from false to true this will rebuild the whole widget tree

and for this work you need use a ternary operator in your in your Scaffold()

So this is how you go

void getLocation() async {
  await location.getCurrentLocation();
  *latlng* = LatLng(location.latitude, location.longitude);
   setState(() {
      coordAvbl = true;
     
    });
}

and modify your Scaffold as

override
 Widget build(BuildContext context) {
  return Scaffold(
   body: Stack(
     children: [
       coordAvbl ? GoogleMap(
         onMapCreated: _onMapCreated,
         mapType: MapType.normal,
         initialCameraPosition: CameraPosition(
           target: *latlng*,
           zoom: 14.4746,
         ),
       ) : Container(),
    ]

I guess you have missed some Brackets but this how you do so if the coordAvbl = true then it will show the map I guess or else it will show an empty container.

fell free to comment if you get any errors

regards, roshan

vIvId_gOat
  • 358
  • 2
  • 13