Chuyển đến nội dung chính

Bài đăng

Hiển thị các bài đăng có nhãn Images

The Flutter image pipeline: from a URL to pixels on screen

Image.network(url) is one of the friendliest APIs in Flutter and one of the easiest to be defeated by. It works immediately, and then a month later you have images that flash white on every rebuild, a gallery that reloads on scroll-back, and 400 MB of memory on a device with 3 GB. All three are the same misunderstanding: Image is a widget, but the caching and decoding happen in an object it delegates to — the ImageProvider . Knowing what that object keys on, and what it caches, explains everything. The five stages Key. The ImageProvider produces a key — for NetworkImage , the URL plus scale. Two providers with equal keys are the same image as far as the cache is concerned. Fetch. Bytes come from the network, an asset bundle, a file, or memory. Decode. Compressed bytes become a dart:ui.Image : raw pixels, width × height × 4 bytes. Cache. The decoded image goes into PaintingBinding.instance.imageCache , keyed on step 1. Paint. The widget draws it, applying fit , alignmen...