Search code examples
androidflutterhttpserver

How to create HTTP server app in flutter?


I am working in Flutter, I have to create an Flutter app which create HTTP server, and serves our local phone storage. I am unable to find any Flutter plugin related to creating HTTP server in flutter, here is sample app, which I found in Play Store, it implements a HTTPS server:

Screenshot of that app

Here is how the app exposes the phone storage via HTML pages:

enter image description here

How I can create this app in Flutter? Please suggest any plugin for that.


Solution

  • It's possible without any third-party packages. Dart's io package provides functionalities to work with file, socket, http and other I/O related things. You can start listening for HTTP requests on a specific address and port using HttpServer.bind. Here is a snippet I found (link):

    startServer() async {
      var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080);
      print("Server running on IP : " +
          server.address.toString() +
          " On Port : " +
          server.port.toString());
      await for (var request in server) {
        request.response
          ..headers.contentType =
              new ContentType("text", "plain", charset: "utf-8")
          ..write('Hello, world')
          ..close();
      }
    }