6

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.

2 Answers 2

12

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();
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks I added the link to my answer @ArtoBendiken
Add internet permission in Android Manifest ` <uses-permission android:name="android.permission.INTERNET"/> `
How to read the payload?
What happens with the UI if there's such a server loop? Will it block the UI?
|
0

I'm researching along a similar line, although I don't want to expose the file system, but rather communicate some data.

  1. A lot of packages are not geared to use within a mobile application (even though Android and iOS is a supported platform). These packages are meant to be used in a server side settings, preferably command line executable app. Some examples: https://pub.dev/packages/local_assets_server or https://pub.dev/packages/dhttpd or https://pub.dev/packages/belatuk_http_server
  2. Regarding serving the root file system: you gonna have some serious difficulties if you'd want to release such an app to the Play Store. Especially the last few versions of Android accessing broader set of folders and files. The MANAGE_EXTERNAL_STORAGE is extremely hard to justify and release or keep the app in the store (it's as hard as justify background location permission or satisfy kids audience policies). Let's admit that it's quite dangerous from security point of view to expose all of these folders and files. If Play Store is not your goal and you are OK with alternative distribution channels then this is not as much of a problem.
  3. Angel3 (https://pub.dev/packages/angel3_framework) seems to be also CLI oriented (although lists Android as a platform). I'll try Alfred (https://pub.dev/packages/alfred), https://pub.dev/packages/mini_server, and https://pub.dev/packages/http_multi_server although these are suspicious as well.

Here is an Medium post: https://medium.com/@naik.rpsn/http-server-running-on-a-mobile-app-with-flutter-1ef1e717dda1. Even though it's 4 years old concepts could apply still.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.