-
Notifications
You must be signed in to change notification settings - Fork 459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SVGs loaded from asset bundles aren't cached #33
Comments
I'm not opposed to using You could, of course, also choose to load and cache your assets in any other way and just use |
Thanks. I've ended up using But, I do know that before I did this, the assets were being loaded pretty much on every widget render. I added debug logging to the code in What got me looking into this in the first place was that my app was regularly crashing when I switched pages too quickly — it seems that there is a race condition of sorts in |
To be clear, the previous iteration of my code was using |
That seems strange. Can you provide an example to reproduce this behavior? There's caching logic that should kick in after the parsing is done. It cache's a |
I lost track of this for a while, sorry. I see the problem now. I was incorrectly assuming that the caching I was doing would work in this case, when it clearly doesn't. This should be fixable without requiring your work around. |
Or not... Can you provide a simple way to reproduce this? I suspect something you're doing in your app ends up clearing the cache unexpectedly. |
Ok — so I changed back to using
I also added a print line in flutter/lib/src/services/asset_bundle.dart
I find that SVGs load repeatedly, as per debug output:
|
Right, this is what I'm not able to reproduce. I added some similar print statements (plus a couple others). I onl ever get one |
Can you share a small app that reproduces this? |
Ok, I've figured out a part of this. My code uses For reasons I don't understand, I'm getting different Because the picture cache caches based on asset key, color, and blend mode, each time I'm rendering this, I get a cache miss. This results in an asset load, and another picture added to the cache. It is currently a mystery to me why IconTheme is behaving this way. |
Ahhh that's actually expected. I suppose it would make sense to cache the asset for this case - I didn't really have that in mind, but I suppose it'd particularly be beneficial for icons where you want to have different colors with the same icon in multiple places. |
I'd like to have some way to make this optional though, as I think there are plenty of use cases where you don't want to keep the SVG data around in memory longer than necessary (e.g. load a large SVG string and draw it in only one color). |
Okay, I figured it out. _TabStyle is an AnimatedWidget. It animates tabs, from material/tabs.dart. It lerps the color, and then builds its own child wrapped inside an IconTheme. So, if the child is an svg, it's going to be loading many of them — one per frame I guess. Each will have a different color. |
Given that this thing with tabs is so counter intuitive, and that image assets are usually cached in Flutter, maybe it makes sense to have SVG assets cached by default? |
This would end up happening with any animation involving color changes/lerps then. Ok. I can buy that. If someone still wants these to not cache, they can use the same work around you're doing to get them to cache in reverse - e.g. load it as a string without caching it and calling |
Fixed in v0.7.0+1 which is 99feec0 |
I'm seeing that SVGs loaded as assets don't get cached at all, because
key.bundle.load(key.name)
doesn't cache, whereaskey.bundle.loadString(key.name)
does.If I'm using SVGs to render icons, this means that the asset is loaded repeatedly as the app is used.
Would it make sense to use loadString instead?
The text was updated successfully, but these errors were encountered: