flame-engine/flame

Sprite.load cannot be used in try catch

Closed this issue · 3 comments

What happened?

I'm trying to load an image with this fail safe try catch and run in debug mode:

    try {
      bgSprite = await Sprite.load(noExistImage);
    } catch (e) {
      bgSprite = await Sprite.load(defaultImage);
    }

When the first load fails, it jumps to the second load, but the second stuck in the Error._throw.

Image

What do you expect?

When the first load fails, the second one should successfully load the default image.

How can we reproduce this?

No response

What steps should take to fix this?

No response

Do have an example of where the bug occurs?

No response

Relevant log output

Execute in a terminal and put output into the code block below

Output of: flutter doctor -v

Affected platforms

Android

Other information

No response

Are you interested in working on a PR for this?

  • I want to work on this

Have you made sure that the default loading works outside of the try-catch?

Yes, I'm sure that the default loading works.
For more information, I'm only working on debug mode.
When the app got stuck, I check the value of the exception in Error._throw and it says that it could not load the first one.
I tried to press F5 in VS code several times and it continues to run and load the default image successfully.

I think you have just set your IDE to pause on exceptions, this isn't anything Flame specific.
You can try the same thing in pure Flutter/Dart code and see if you get the same behavior.

var number = 0;
try {
  throw Exception();
} catch (e) {
  number = 1;
}