Skip to content

A simple handler for the Shelf ecosystem to serve files from Flutter assets.

License

Notifications You must be signed in to change notification settings

r8/shelf_flutter_asset

Repository files navigation

shelf_flutter_asset

build status codecov pub package package publisher

A simple handler for the Shelf ecosystem to serve files from Flutter assets.

Usage

Bind as root handler:

import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_flutter_asset/shelf_flutter_asset.dart';

void main() {
  var assetHandler = createAssetHandler(defaultDocument: 'index.html');

  io.serve(assetHandler, 'localhost', 8080);
}

Bind with shelf_router:

import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;

void main() {
  var app = Router();
  final assetHandler = createAssetHandler();

  app.get('/hello', (Request request) {
    return Response.ok('hello-world');
  });

  app.get('/assets/<ignored|.*>', (Request request) {
    return assetHandler(request.change(path: 'assets'));
  });

  io.serve(app, 'localhost', 8080);
}

Bind with shelf_router and custom root path:

import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;

void main() {
  var app = Router();
  final assetHandler = createAssetHandler(rootPath: 'assets/html');

  app.get('/hello', (Request request) {
    return Response.ok('hello-world');
  });

  app.get('/assets/<ignored|.*>', (Request request) {
    return assetHandler(request.change(path: 'assets/html'));
  });

  io.serve(app, 'localhost', 8080);
}