Skip to main content
Version: 6.x.x

In-App Localhost Server

The InAppLocalhostServer class allows you to create a simple server on http://localhost:[port]/ in order to be able to load your assets file on a local server.

Basic Usage

To start and stop the server, you can use the InAppLocalhostServer.start and InAppLocalhostServer.stop methods.

The default port value is 8080.

Use directoryIndex to change the index file to use. The default value is index.html.

Use documentRoot to change the document root path to serve. The default value is ./.

Example:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

final InAppLocalhostServer localhostServer =
InAppLocalhostServer(documentRoot: 'assets');

Future main() async {
WidgetsFlutterBinding.ensureInitialized();

if (!kIsWeb) {
// start the localhost server
await localhostServer.start();
}

if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
await InAppWebViewController.setWebContentsDebuggingEnabled(kDebugMode);
}

runApp(const MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
const MyApp({super.key});


State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('InAppLocalhostServer Example'),
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: InAppWebView(
initialSettings: InAppWebViewSettings(
isInspectable: kDebugMode,
),
initialUrlRequest:
URLRequest(url: WebUri("http://localhost:8080/index.html")),
onWebViewCreated: (controller) {},
onLoadStart: (controller, url) {},
onLoadStop: (controller, url) {},
),
)
])),
);
}
}