trying to deploy shelf package on flutter mobile without succes

Hello, i am trying to build a mobile application for a universty subject and i am new to both dart and flutter. I am having trouble setting up a local http server that i want to be hosted by the movile phone so other devices connected to the same network can communicate with it. I am using the shelf package to deploy it. My regard is that when i run the flutter app on the andorid emulator everything initializes correctly and on the terminal i get the “Server running on http://${server.address.address}:${server.port}” without catching any error, and when i try to communicate with the server through a different terminal to the one i did flutter run i get this error "curl : No es posible conectar con el servidor remoto
En línea: 1 Carácter: 1

  • curl http://0.0.0.0:8083
  •   + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:  
     HttpWebRequest) [Invoke-WebRequest], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.Power  
     Shell.Commands.InvokeWebRequestCommand"
    
    

I tried to search on the toppic but found no information that helped me, does anyone know what the error could be?

This is my code:

dart server file:

import 'dart:io';
import 'dart:isolate';

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_router/shelf_router.dart';

void startServer(SendPort sendPort) async {
  final app = Router();

  // Define routes
  app.get('/hello', (Request request) {
    return Response.ok('Hello, world!');
  });

  app.get('/user/<name>', (Request request, String name) {
    return Response.ok('Hello, $name!');
  });

  //var ip = await _getLocalIpAddress();

  try {
    // Bind the server to a port
    final server = await shelf_io.serve(app, '0.0.0.0', 8083);
    print('Server running on http://${server.address.address}:${server.port}');
    sendPort.send('Server is running!');
  } catch (e) {
    sendPort.send('Error starting server: $e');
  }
}

Future<void> runServerInIsolate() async {
  final receivePort = ReceivePort();

  await Isolate.spawn(startServer, receivePort.sendPort);

  receivePort.listen((message) {
    print(message); // Log the message from the isolate
  });
}

i tried without the isloation and it didn’t work either.

Flutter app:

import 'package:demo_app/pages/webservertest.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
  runServerInIsolate();
  //startServer();
}

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(),
    );
  }
}

Because 0.0.0.0 is not a valid end-point!

RFC1700, page 3, paragraph a:

(a) {0, 0}

     This host on this network.  Can only be used as a source
     address (see note later).

In other words:

0.0.0.0 can only be used in TCP listeners. It means that it will bind the listeners to all available IPv4 on that interface (or in all interfaces, it doesn’t matter in this context).

What you need is an end-point: a public address for that machine (127.0.0.1 is this machine, 192.168.1.34 is an example of a machine in the same network, 200.246.10.2 is an example of a machine in the internet.

So, basically, change your http://0.0.0.0:8083 to http://localhost:8083 (or whatever DNS/IP Address you have for your local network that points to your server).

Notice that iOS simulator can access 127.0.0.1 (localhost). Android emulator cannot (but they can access your host machine through 10.0.2.2). Anything else will need a local-area network address (your ethernet or wifi) or a wide-area network address (an internet address).

Thank for you´r reply! I have tried by setting my app to localhost too and when trying to connect it doesn´t work either. I am not sure at what do you mean by " Android emulator cannot (but they can access your host machine through 10.0.2.2)". Do you mean that i have to set my server ip to localhost and when trying to connect use “curl http://10.0.2.2:8080”. To clarify myself i am trying to connect to the server hosted in the android emulator from VS code using curl on the powershell.

IP Address is a unique-ish address that represents one machine on the local network or internet.

Your computer + your Android are connected though your local network. Each of these devices has, at least, 2 IP addresses: 127.0.0.1 is the LOCAL machine. It can only be used in the SAME machine. If you are trying to reach the Android phone/emulator from your machine, you’ll need to provide its IP Address.

You can get your Android emulator or device IP by running this:

adb shell
ifconfig eth0

The first command will open a shell to your Android device/emulator (I guess your device is already has developer options enabled, since you can debug Flutter apps on it, right?)

The second command will list all IP addresses for the first ethernet controller.

It will show you something like this:

eth0: ip 10.0.2.15 mask 255.255.255.0 flags [up broadcast running multicast]

So, for your PC to connect to the Android server, it should be 10.0.2.15:8083.

I’m assuming those two devices are in the same network (for emulator, it only works if the adb can reach the device).