Flutter未处理的异常:SocketException:操作系统错误:连接被拒绝,errno = 111

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter未处理的异常:SocketException:操作系统错误:连接被拒绝,errno = 111相关的知识,希望对你有一定的参考价值。

我正在尝试使用android studio开发的flutter应用程序,以连接到本地托管的C#API。在IIS Express上运行api会提供一个https://localhost:44364/api/trip的URL,我认为该URL与使用模拟器的Android Studio中的http://10.0.2.2:44364/api有关。

我正在尝试通过API从数据库中获取行程列表。邮递员工作正常。

我遇到多个错误。我尝试了模拟器,也尝试了直接插入电话并将地址切换到我的IP地址。我什至尝试将api切换为在控制台而不是IIS中运行。无效。

我确实尝试通过仿真器浏览器访问api,并被告知该站点不安全,导致我认为这是证书问题。

flutter项目非常简单,处于起步阶段,我仍在学习如何对其进行编程。

这里是三个颤动文件和错误列表。

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;
  

控制台

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!
答案

主要是当您的android AVD或android设备未连接到互联网时,才会发生此错误。有时,开发人员尝试从android设备而非AVD访问localhost上的插件,在这种情况下,会发生相同的错误。我希望这能解决您的问题。

[其他情况涉及端口被服务器端的防火墙阻止。

如果您认为我的回答是有用的,请接受好评。

Learn Cybers

以上是关于Flutter未处理的异常:SocketException:操作系统错误:连接被拒绝,errno = 111的主要内容,如果未能解决你的问题,请参考以下文章

未处理的异常:NoSuchMethodError:方法'showNotificationDaily'在flutter中被调用为null

Flutter:未处理的异常:错误状态:DocumentSnapshotPlatform中不存在字段

Flutter,无法提取 api 数据:(未处理的异常:NoSuchMethodError:方法 'map' 在 null 上被调用。)

未处理的异常:MissingPluginException(未找到对通道 plugins.flutter.io/connectivity 进行方法检查的实现)

Flutter 中的错误:未处理的异常:“Null”类型不是“String”类型的子类型

未处理的异常:MissingPluginException(对于 Flutter 上的许多包,在通道 plugins.flutter.io/ 上找不到方法 getAll 的实现