Can't Load Current location in Flutter application

后端 未结 3 865
情歌与酒
情歌与酒 2021-01-14 20:31

I\'m using geolocator plugin and getting current latitude and longitude but i can\'t load that in initstate of my Flutter Application. It showing Render Error.



        
3条回答
  •  感动是毒
    2021-01-14 21:10

    I have almost no idea what's happening but based on the code since you have a .then, before the .then function happen your latitude and longitude are null, and when you have a .then the rest of your code is not awaiting for the future to be resolved. Try initializing the longitude and latitude to some value other than null in your init state so:

    void initState() {
    // TODO: implement initState
    super.initState();
    latitude = 0;
    longitude = 0;
    getCurrentLocation().then((k) {
      latitude = k.latitude;
      longitude = k.longitude;
      setState(() {});
      });
    }
    

提交回复
热议问题