简体   繁体   中英

Why can't I call these calculations into my build, when I'm already doing it for another function

The var _distancebetween is causing issues here. I don't know how to call it in my widget Build:

 void _onCameraMove(CameraPosition cameraPosition) async {
    
    var _distancebetween = await Geolocator().distanceBetween(
        lat, lng, _lastMapPosition.latitude, _lastMapPosition.longitude);
    
    _lastMapPosition = cameraPosition.target;
    var latlnglastPosition = _lastMapPosition.toString;

    setState(() {
      latlnglastPosition;
      _distancebetween;
      _lastMapPosition;
    });
  }

when I setState for _latlnglastPosition, and _lastMapPosition, I call them in my build like so:

  @override
  Widget build(BuildContext context) {

    var latlnglastPosition = _lastMapPosition.toString();
    

and I can easily display the text in an Ink Response field.

Why won't this work for _distancebetweenFinal?

In my build:

var _distancebetweenFinal = _distancebetween.toString();

print(_distancebetweenFinal);
print(_distancebetween);

This prints zeros... But if printed from my void _onCameraMove, it's printing the correct value. How can I pass the value from _onCameraMove into my widget build?

Declare your _onCameraMove() in initState could work, like this:

  @override
  void initState() { 
    super.initState();
    _onCameraMove();
  }

And make sure you make your class Statefull and call initState before build method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM