I'm attempting to get a flutter app, made in android studio, to connect to a c# api hosted locally. Running the api on IIS Express gives a url of https://localhost:44364/api/trip, which I believe relates to http://10.0.2.2:44364/api in Android studio when using the emulator.
I'm attempting to retrieve a list of Trips from the db via API. Postman works fine.
I get multiple errors. I have tried the emulator, I've also tried plugging a phone in directly and switching the address to my ip address. I've even tried switching the api to run in the console and not IIS. Nothing works.
I did attempt to reach the api through the emulator browser and was told the site was unsafe, leading me to think it's a certificate problem.
The flutter project is fairly simple, it's in its infancy stage and I'm still learning how to program it.
Here are the three flutter files, and the error list.
main.dart
import 'dart:convert';
import 'package:bike_client/API.dart';
import 'package:bike_client/models/Trip.dart';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Track It',
theme: new ThemeData(
primarySwatch: Colors.red,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
createState() => new _MyHomePageState();
}
class _MyHomePageState extends State {
var trips = new List<Trip>();
_getTrips() {
API.getTrips().then((response) {
setState(() {
Iterable list = json.decode(response.body);
trips = list.map((model) => Trip.fromJson(model)).toList();
});
});
}
initState() {
super.initState();
_getTrips();
}
dispose() {
super.dispose();
}
@override
build(context) {
return Scaffold(
appBar: AppBar(
title: Text("Trip List"),
),
body: ListView.builder(
itemCount: trips.length,
itemBuilder: (context, index) {
return ListTile(title: Text((trips[index].tripId).toString()));
},
));
}
}
API.dart
import 'dart:async';
import 'package:http/http.dart' as http;
//const baseUrl = "https://192.168.1.14:44364/api";
const baseUrl = "http://10.0.2.2:44364/api";
class API {
static Future getTrips() {
var url = baseUrl + "/trip/1";
return http.get(url);
}
}
Trip.dart
class Trip {
int tripId;
int userId;
Trip(int tripId, int userId) {
this.tripId = tripId;
this.userId = userId;
}
Trip.fromJson(Map json)
: tripId = json['tripId'],
userId = json['userId'];
Map toJson() {
return {'tripId': tripId, 'userId': userId};
}
}
Console
Launching lib\main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...
E/flutter ( 7781): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 38612
E/flutter ( 7781): #0 IOClient.send (package:http/src/io_client.dart:33:24)
E/flutter ( 7781): <asynchronous suspension>
E/flutter ( 7781): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:176:38)
E/flutter ( 7781): #2 BaseClient.get (package:http/src/base_client.dart:35:7)
E/flutter ( 7781): #3 get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter ( 7781): #4 _withClient (package:http/http.dart:166:20)
E/flutter ( 7781): #5 get (package:http/http.dart:46:5)
E/flutter ( 7781): #6 API.getTrips (package:bike_client/API.dart:9:12)
E/flutter ( 7781): #7 _MyHomePageState._getTrips (package:bike_client/main.dart:30:9)
E/flutter ( 7781): #8 _MyHomePageState.initState (package:bike_client/main.dart:40:5)
E/flutter ( 7781): #9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
E/flutter ( 7781): #10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #13 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #31 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #32 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #35 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #38 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #39 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #42 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #43 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #44 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #45 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #46 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #47 Element.updateChild (package:flutter/src/wid
E/flutter ( 7781): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 38760
E/flutter ( 7781): #0 IOClient.send (package:http/src/io_client.dart:33:24)
E/flutter ( 7781): <asynchronous suspension>
E/flutter ( 7781): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:176:38)
E/flutter ( 7781): #2 BaseClient.get (package:http/src/base_client.dart:35:7)
E/flutter ( 7781): #3 get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter ( 7781): #4 _withClient (package:http/http.dart:166:20)
E/flutter ( 7781): #5 get (package:http/http.dart:46:5)
E/flutter ( 7781): #6 API.getTrips (package:bike_client/API.dart:9:12)
E/flutter ( 7781): #7 _MyHomePageState._getTrips (package:bike_client/main.dart:30:9)
E/flutter ( 7781): #8 _MyHomePageState.initState (package:bike_client/main.dart:40:5)
E/flutter ( 7781): #9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
E/flutter ( 7781): #10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #13 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #31 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #32 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #35 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #38 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #39 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #42 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #43 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #44 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #45 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #46 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #47 Element.updateChild (package:flutter/src/wid
E/flutter ( 7781): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 38884
E/flutter ( 7781): #0 IOClient.send (package:http/src/io_client.dart:33:24)
E/flutter ( 7781): <asynchronous suspension>
E/flutter ( 7781): #1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:176:38)
E/flutter ( 7781): #2 BaseClient.get (package:http/src/base_client.dart:35:7)
E/flutter ( 7781): #3 get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter ( 7781): #4 _withClient (package:http/http.dart:166:20)
E/flutter ( 7781): #5 get (package:http/http.dart:46:5)
E/flutter ( 7781): #6 API.getTrips (package:bike_client/API.dart:9:12)
E/flutter ( 7781): #7 _MyHomePageState._getTrips (package:bike_client/main.dart:30:9)
E/flutter ( 7781): #8 _MyHomePageState.initState (package:bike_client/main.dart:40:5)
E/flutter ( 7781): #9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
E/flutter ( 7781): #10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #13 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #31 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #32 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #35 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #38 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
E/flutter ( 7781): #39 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
E/flutter ( 7781): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
E/flutter ( 7781): #42 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
E/flutter ( 7781): #43 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
E/flutter ( 7781): #44 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
E/flutter ( 7781): #45 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
E/flutter ( 7781): #46 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
E/flutter ( 7781): #47 Element.updateChild (package:flutter/src/wid
E/flutter ( 7781): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: SocketException:
AND MUCH MORE!
You're trying to bind to port 80, which needs root privileges. Have you tried running the server as a port >1024? It seems that every port here needs root privileges.
Flutter SocketException (SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 51500) Flutter SocketException is thrown when a socket operation fails. Implemented types. IOException. Constructors.
This error means that the client cannot connect to the port on the computer running server script. This can be caused by few things, like lack of routing to the destination or you have a firewall somewhere between your client and the server - it could be on server itself or on the client etc.
This error occurs mostly when your android AVD or android device is not connected to internet. Sometimes, the developer tries to access plugin on localhost from android device instead of AVD, in such cases, same error occurs. I hope this solves your problem.
Other scenarios involves port blocked by firewalls on serverside.
Kindly upvote if you find my answer usable.
Learn Cybers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With