Member-only story
In this tutorial I will show you how to set up both a Dart server and a Flutter app that interacts with it. The server code will run on your local machine and the Flutter app will run in the Android emulator or iOS simulator.
I’m assuming that you have a Flutter set up and have at least a little experience making Flutter apps.
Server-Side Dart
We will start by setting up the server.
Since you have Flutter, the Dart SDK is already installed. It is located in your Flutter SDK directory here:
<flutter sdk directory>/bin/cache/dart-sdk/bin
Update your system’s environment path to include this location.
Create a directory for your server app. Let’s call it dart_server
.
mkdir dart_server
cd dart_server
Create a new file called server.dart and open it in your favorite editor. Let’s paste in a slightly modified version from the documentation:
import 'dart:io';
Future main() async {
var server = await HttpServer.bind(
InternetAddress.loopbackIPv4,
3000,
);
print('Listening on localhost:${server.port}');
await for (HttpRequest request in server) {
request.response
..write('Hello…