Resources from the network

An http: or https: resource of the ResourceLoader (a Resource of @Value, getResource, a ResourceView, a ResourceImage) is downloaded once and kept in the app's files: read from the disk from then on, across starts too, and asked again in the background past its age.

Why

On iOS, Android and the desktops Image.network keeps an image in memory only: after every start the avatars and pictures are downloaded again. In the browser its HTTP cache keeps them, but only as the server's headers allow. A resource kept in the app's files is there at the next start without the network, and is checked with the server only now and then.

Example

spring:
  flutter:
    resources:
      cache:
        max-size: 300MB          # the kept resources, together
        max-age: 7d              # past it: used, and asked again
        max-entry-size: 50MB     # larger: from the network every time
        locations:               # the first rule that matches
          - pattern: https://avatars.steamstatic.com/**
            max-age: 1d
          - pattern: https://api.example.com/**
            enabled: false
      view:
        fade: 200ms
// Any resource: an image now, a type of the app's own by a renderer bean.
ResourceView(
  player.avatarUrl,
  width: 40,
  height: 40,
  fit: BoxFit.cover,
  shape: const CircleBorder(),
  placeholder: const AvatarPlaceholder(),
  error: (context, error) => const AvatarPlaceholder(),
)

// For Flutter's widgets that take an ImageProvider.
CircleAvatar(backgroundImage: ResourceImage(player.avatarUrl))

How it works

@Component()
class PdfRenderer implements ResourceRenderer {
  @override
  bool supports(String mediaType) => mediaType == 'application/pdf';

  @override
  Widget build(BuildContext context, LoadedResource resource,
          ResourceLayout layout) =>
      PdfView(bytes: resource.bytes);
}

A video with the player you chose

The framework has no player: a kept resource gives playableUri(), the address a player can play it from: the kept file (file:), a blob: of it in a browser, or, the first time, its own address while it is being kept for the next time.

final clip = resources.getResource(highlightUrl) as CachedResource;
final uri = await clip.playableUri();
final controller = uri.scheme == 'file'
    ? VideoPlayerController.file(File(uri.toFilePath()))
    : VideoPlayerController.networkUrl(uri);
await controller.initialize();

A single file is kept (mp4, webm); a playlist of pieces (HLS, DASH) is not, and a file still being written is played from the network.

Properties

Key Default Meaning
spring.flutter.resources.cache.enabled true Whether http(s) resources are kept
spring.flutter.resources.cache.max-age 7d How long one is used as it is
spring.flutter.resources.cache.max-entry-size 50MB The largest one kept
spring.flutter.resources.cache.max-size none The most bytes the kept resources take, together
spring.flutter.resources.cache.locations none Rules by address: pattern, max-age, enabled
spring.flutter.resources.view.fade 200ms The fade of a ResourceView