I attemplted to start a local webserver on Android using Flutter app. I'm using shelf & shelf_static.
I've put index.html
file in the assets
folder of the root project folder. Then, I referenced it in the pubspec.yaml file.
assets:
- assets/
Then, I made a button to call this function:
Future<void> _startShelfLocalhostServer() async {
// Serve the `web` directory.
var handler = createStaticHandler('assets', defaultDocument: 'index.html');
// Create a Shelf cascade with the static file handler first, and the fallback handler second.
var cascade = Cascade().add(handler).add(_echoRequest);
// Start the server on port 8080.
var server = await io.serve(cascade.handler, 'localhost', 8080);
// Print the URL to the console.
print('Server listening on ${server.address.host}:${server. Port}');
}
I expect that I can open the HTML file when I go to localhost:8080, but instead, I got this error:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): A directory corresponding to fileSystemPath "assets" could not be found
So, how do I fix it?