Yadu
Yadu

Reputation: 3305

Server using flutter

As we all know Flutter is great for the front end, as I got into working with flutter I liked the Dart programming language very much I used it on some backend servers as well, Now it got me thinking if I can do this..

I have a aqueduct server (Aqueduct is a dart package, which is very similar to express on node.js)

import 'dart:async';
import 'dart:io';
import 'package:aqueduct/aqueduct.dart';
import 'package:aqueduct/managed_auth.dart';

Future main() async {
  final app = Application<App>()
    ..options.configurationFilePath = 'config.yaml'
    ..options.port = 8888;

  await app.start(numberOfInstances: 3);
}

class App extends ApplicationChannel {
//server side logic
}

will the server/app be successfully built if I make the above main() as the entry point of the flutter app, successfully making the flutter app running on the android device to act as a server?

Or

how can I make it work if the above code fails?

PS: I have not tried this yet. for your information: a node.js express server can be (may be) run on android using Node.js ARM

Upvotes: 0

Views: 871

Answers (2)

Yadu
Yadu

Reputation: 3305

The below code runs a server on the port 4040 inside a flutter app

import 'dart:io';

Future main() async {
  // #docregion bind
  var server = await HttpServer.bind(
    InternetAddress.loopbackIPv4,
    4040,
  );
  // #enddocregion bind
  print('Listening on localhost:${server.port}');

  // #docregion listen
  await for (HttpRequest request in server) {
    request.response.write('Hello, world!');
    await request.response.close();
  }
}

Upvotes: 5

Uncle Max
Uncle Max

Reputation: 19

if all your liberies is exiting on the flutter phone compiler your server will run without problem probably but if it is not you can looking for anther libery or try to download the libery source code and put it with your dart files

NOTE : I wanna know why you hadn't try to do an small experiment to see if it's work before posting this qustion

Upvotes: 0

Related Questions