Search code examples
androidflutterdartwidgetibeacon

How to Update Variables Between Widgets?


I want to continuously update the x and y values in the TabScanning widget and use these updated values in the MapPage widget. When I print x and y on the TabScanning page, the values are updated. But the values in MapPage always remain 0.0.

https://github.com/zehradogru/fyp_new

TabScanning Widget:

class TabScanning extends StatefulWidget {
  double x = 0, y = 0;
}

class _TabScanningState extends State<TabScanning> {

  initScanBeacon() async {
    await flutterBeacon.initializeScanning;
        ..
        ...
        setState(() {
        ..
        ...
            widget.x = (c * e - f * b);
            widget.x = widget.x / (e * a - b * d);

            widget.y = (c * d - a * f);
            widget.y = widget.y / (b * d - a * e);
        });
      }
    });
  }

MapPage Widget:

class _MapPageState extends State<MapPage> {

  TabScanning bec = TabScanning();
  double x = 0;
  double y = 0;
  Offset location = const Offset(0, 0);

  @override
  void initState() {
    super.initState();
    Timer.periodic(const Duration(milliseconds: 20), (timer) {
      setState(() {
        x = bec.x;
        y = bec.y;
        location = Offset(x,y);
      });
    });
  }

Solution

  • For observable data shared between widgets, you will need a state management solution. See what flutter.dev has to say about that.