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(),
);
}
}