1

I'm trying to test out a button in Flutter using the test Flutter lib. I use the following code for the test:

await tester.tap(find.widgetWithText(GestureDetector, "ref size"));
expect(testContainerState.childWidth, 200.0);

When tapping on the button, the following function gets invoked:

  void setToRefSize() async {
    print("SETTING REF SIZE (0)");
    ui.Image img = await widget.referenceImages[referenceImageIndex].getImageData();
    print("SETTING REF SIZE (1)");
  }

But for some reason, only the first print statement produces output. I'm pretty sure that it has to do with this being async

ui.Image img = await widget.referenceImages[referenceImageIndex].getImageData();

The getImageData() method is defined as follows:

  Future<ui.Image> getImageData() async {
    Completer<ui.Image> completer = new Completer<ui.Image>();
    image
      .resolve(new ImageConfiguration())
      .addListener((ImageInfo info, bool _) => completer.complete(info.image));
    return completer.future;
  }

The strange thing is that all of this works when testing the button manually, so just running it on a device and tapping the button myself.

EDIT Taking a look at it again, the problem might be that the listener on the image in the getImageData() method is not done in sync. Still not certain on how to fix it though.

2
  • I think there is an open issue to make assets available in tests that might be related Commented Mar 31, 2018 at 14:44
  • Adding to what Günter said, if you have a small project that replicates the problem, consider posting an issue on the Flutter repo (github.com/flutter/flutter). Commented Apr 3, 2018 at 20:27

1 Answer 1

1

According to the research I've done on this subject, flutter tests supports waiting for async methods calls but doesn't support awaiting async callbacks.

When you tap the button, the action is done immediately, the button then triggers his callback and causes the async suspension, because the tester is already on the next line, but the callback is not yet finished.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.