http
http copied to clipboard
Unhandled Exception: Connection failed
when I run my flutter on macos(m2) the exception occurred
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Connection failed
#0 IOClient.send (package:http/src/io_client.dart:88:7)
<asynchronous suspension>
#1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:93:32)
<asynchronous suspension>
#2 _withClient (package:http/http.dart:164:12)
<asynchronous suspension>
#3 Request.request (package:daily_diet_ui/utils/request/request.dart:27:18)
<asynchronous suspension>
#4 SharedFunction.getCaptcha (package:daily_diet_ui/utils/storage/shared_function.dart:51:5)
<asynchronous suspension>
#5 _LoginState.getCaptcha (package:daily_diet_ui/pages/login.dart:32:15)
<asynchronous suspension>
the back-server is running on my mac,too. when I run my flutter on macos(m2) , no matter what ip (my mac ip:8756 , 127.0.0.1:8756 , 10.0.2.2:8756) I set ,the error comes. but when I run the app in android emulator it works fine
the minimal demo
request.dart
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
class Request {
Future request(
String path, Map<String, dynamic> queryParameters, String method) async {
String authority = '127.0.0.1:8756';
var url = Uri.http(authority, path, queryParameters);
late http.Response response;
Map<String, String> header = {
"Content-Type": "application/json"
};
if (method == 'get') {
response = await http.get(url, headers: header);
} else if (method == 'post') {
response = await http.post(url, headers: header);
}
debugPrint(response.body);
}
Future get(String path, {Map<String, dynamic> queryParameters = const {}}) {
return request(path, queryParameters, 'get');
}
}
main.dart
import 'package:flutter/material.dart';
import 'package:testqwe/request.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
Request().get("/tool/GenerateCaptcha").then((res) {
debugPrint(res);
});
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
then run on macos and click the add button
env http: ^0.13.5 flutter 3.3
I had the same issue. My problem was the macOS permissions were not set in the respective entitlements files to allow network client access for the macOS build.
Adding the following to macos/Runner/DebugProfile.entitlements and macos/Runner/Release.entitlements fixed my issue.
<key>com.apple.security.network.client</key>
<true/>
I had the same issue. My problem was the macOS permissions were not set in the respective entitlements files to allow network client access for the macOS build.
Adding the following to
macos/Runner/DebugProfile.entitlementsandmacos/Runner/Release.entitlementsfixed my issue.<key>com.apple.security.network.client</key> <true/>
That point isn't too obvious from Apple Dev docs, neither it uses the same pattern as Info.plist in iOS apps, thank you!
It's problem with unsecured http protocol - that's commonly problem not only in flutter, also in dart - simple code on dart can make requests with unsecured http, but client just breaking on the second one. Best way is to use https. If u are creating ur own project just use ngrok for secure tunneling. At least this solved problem for me)) + Android will force you to use https in all cases))