-1

Thank you in advance for looking into this problem I'm having issues just doing popping back to a page!

I have this code that works in one widget. It is scanning a QR code

     @override
     initState() {
      _scan();
      super.initState();
     }

     Future _scan() async {
      barcode = await scanner.scan();
      widget.test();
      Navigator.pop(context);
    }

How would I correctly do it with this other library? Within the callback call the widget.test() and Pop?

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

_captureController.onCapture(
  (data) {
    print('onCapture----$data');

    setState(() {
      _captureText = data;
    });
  },
);

Here is a picture of the error I'm getting enter image description here

Gene
  • 374
  • 3
  • 8
  • 1
    Check this link https://stackoverflow.com/a/49458289/1843853 You can't use the context to get the inherited widget on the initstate. This link gives you some suggestions. – Gabriel Gava Aug 25 '20 at 16:35

1 Answers1

0

So the solution was this. Instead of calling a function directly in setState(), I flipped a bool.

bool _popBack = false;

@override
  initState() {
    _captureController.onCapture(
      (data) {
        print('onCapture----$data');
        setState(() {
          _captureText = data;

          if (!_popBack) {
            _popBack = true;
            _popTime();
          }
        });
      },
    );

And this worked !

Future<dynamic> _popTime() async {
    widget.test();
    Navigator.of(context).pop();
  }

Don't ask me too many questions on why or how. It worked !

Gene
  • 374
  • 3
  • 8