未行程的例外状况:套接字异常:操作系统错误:连接超时,错误号= 110,地址= api.generaliot.in,端口= 45700 flutter

wko9yo5t  于 2022-11-25  发布在  Flutter
关注(0)|答案(1)|浏览(111)
import 'dart:convert';
import 'dart:io';
import 'package:custom_searchable_dropdown/custom_searchable_dropdown.dart';
import 'package:expandable/expandable.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'useful_screens/classes.dart';

import 'useful_screens/constants.dart';
import 'equipments_and_points_details.dart';

class View_equipments extends StatefulWidget {
  const View_equipments({Key? key}) : super(key: key);

  @override
  _View_equipmentsState createState() => _View_equipmentsState();
}

class _View_equipmentsState extends State<View_equipments> {
  List<Department> selected_departments = [];
  Map equipments_dept = {};
  late int len_map;
  late String token;
  late DateTime start;
  bool processing = true;

  void get_departments_list() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    token = prefs.getString('token')!;
    var query_params = {'token': token, 'site_id': '2'};

    Uri url = Uri.parse('$BASE_URL/companies/sites/departments/list')
        .replace(queryParameters: query_params);
    Response response = await http.get(
      url,
      headers: headers,
    );
    List department_ids = jsonDecode(response.body)["department_ids"];

    //getting the list of departments

    var url_1 = Uri.parse('${BASE_URL}/companies/sites/departments/info');
    Response response_1 = await http.post(url_1,
        headers: headers,
        body: jsonEncode({'token': token, 'department_ids': department_ids}));

    var data = jsonDecode(response_1.body)["departments"];
    selected_departments = List.generate(
        data.length,
        (i) => Department(
            id: data[i]['id'], name: data[i]['name'], site: data[i]['site']));
    get_equipments();
  }

  // Future<Map> get_equipments() async {
  //   Map<String, List<Equipment>> equipments_dept = {};
  //   SharedPreferences prefs = await SharedPreferences.getInstance();
  //   String token = prefs.getString('token')!;
  //   for (int i = 0; i < selected_departments.length; i++) {
  //     var query_params = {
  //       'token': token,
  //       'department_id': '${selected_departments[i].id}'
  //     };
  //     var url = Uri.parse(
  //       '${BASE_URL}/companies/sites/departments/equipments/list',
  //     ).replace(queryParameters: query_params);
  //     Response response = await http.get(
  //       // Uri.parse('${BASE_URL}/companies/sites/users/info'),
  //       url,
  //       headers: {
  //         // HttpHeaders.authorizationHeader: "Token $token",
  //         HttpHeaders.contentTypeHeader: "application/json"
  //       },
  //     );
  //
  //     var query_params_1 = {
  //       'token': token,
  //       'equipment_ids': jsonDecode(response.body)['equipment_ids']
  //     };
  //     var url_1 =
  //         Uri.parse('${BASE_URL}/companies/sites/departments/equipments/info');
  //     Response response_1 = await http.post(url_1,
  //         headers: {
  //           // HttpHeaders.authorizationHeader: "Token $token",
  //           HttpHeaders.contentTypeHeader: "application/json"
  //         },
  //         body: jsonEncode(query_params_1));
  //     List equipments = jsonDecode(response_1.body)['equipments'];
  //
  //     equipments_dept[selected_departments[i].name] = List.generate(
  //       equipments.length,
  //       (i) => Equipment(
  //           id: equipments[i]['id'],
  //           name: equipments[i]['name'],
  //           department: equipments[i]['department'],
  //           make: equipments[i]['make'],
  //           model: equipments[i]['model'],
  //           commision_date: equipments[i]['commision_date'],
  //           sump_capacity: equipments[i]['sump_capacity'],
  //           hac_code: equipments[i]['hac_code'],
  //           longitude: equipments[i]['longitude'],
  //           latitude: equipments[i]['latitude']),
  //     );
  //   }
  //   //Now we have the equipments with their respective departments
  //   return equipments_dept;
  // }

  void get_equipments() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String token = prefs.getString('token')!;
    for (int i = 0; i < selected_departments.length; i++) {
      var query_params = {
        'token': token,
        'department_id': '${selected_departments[i].id}'
      };
      Response response = await http.get(
          Uri.parse(
            '${BASE_URL}/companies/sites/departments/equipments/list',
          ).replace(queryParameters: query_params),
          headers: headers);
      // print('equipmentids');
      // print(response.statusCode);
      // print(response.body); //include them in a list and display

      var query_params_1 = {
        'token': token,
        'equipment_ids': jsonDecode(response.body)['equipment_ids']
      };
      Response response_1 = await http.post(
          Uri.parse('${BASE_URL}/companies/sites/departments/equipments/info'),
          headers: headers,
          body: jsonEncode(query_params_1));
      // print(response_1.statusCode);
      // print(response_1.body); //include them in a list and display
      List equipments = jsonDecode(response_1.body)['equipments'];

      equipments_dept[selected_departments[i].name] = List.generate(
        equipments.length,
        (i) => Equipment(
            id: equipments[i]['id'],
            name: equipments[i]['name'],
            department: equipments[i]['department'],
            make: equipments[i]['make'],
            model: equipments[i]['model'],
            commision_date: equipments[i]['commision_date'],
            sump_capacity: equipments[i]['sump_capacity'],
            hac_code: equipments[i]['hac_code'],
            longitude: equipments[i]['longitude'],
            latitude: equipments[i]['latitude']),
      );
      setState(() {
        processing = false;
      });
    }

    //Now we have the equipments with their respective departments
  }

  @override
  Widget build(BuildContext context) {
    get_departments_list();
    return Scaffold(
        appBar: AppBar(
          title: Text('Equipments'),
          centerTitle: true,
        ),
        body: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: !processing
                ? Center(
                    child: SpinKitDoubleBounce(
                      color: Colors.white,
                      size: 100,
                    ),
                  )
                : Column(
                    children: List.generate(
                        selected_departments.length,
                        (i) => dept_dropdown(selected_departments[i].name,
                            equipments_dept[selected_departments[i]], context)),
                  )));
  }
}

Widget dept_dropdown(String dept, List<Equipment>? equipments, context) {
  print(dept);
  print(equipments);
  if (equipments == null) {
    print('empty');
    return Center(
      child: Container(
        margin: EdgeInsets.fromLTRB(0, 20, 0, 10),
        height: 100,
        width: MediaQuery.of(context).size.width * 0.9,
        decoration: BoxDecoration(color: Colors.blue),
      ),
    );
  }
  int len = equipments.length;
  print('Hello');
  return ExpandableNotifier(
    child: Expandable(
      collapsed: Row(
        children: [
          Expanded(
            child: ExpandableButton(
              child: Container(
                // padding: EdgeInsets.symmetric(vertical: 10),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Expanded(
                          child: Text(dept,
                              style: TextStyle(
                                fontSize: 20,
                                fontWeight: FontWeight.bold,
                              )),
                          flex: 8,
                        ),
                        Expanded(
                          child: Icon(Icons.add),
                          flex: 4,
                        ),
                      ],
                    )
                  ],
                ),
              ),
            ),
          )
        ],
      ),
      expanded: Column(
        children: [
          ExpandableButton(
            child: Container(
              // padding: EdgeInsets.symmetric(vertical: 10),
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Expanded(
                        child: Text(
                          dept,
                          style: TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        flex: 8,
                      ),
                      Expanded(
                        child: Icon(Icons.minimize_outlined),
                        flex: 4,
                      ),
                    ],
                  )
                ],
              ),
            ),
          ),
          Column(
              children: List.generate(
                  len,
                  (i) => InkWell(
                        onTap: () {
                          Navigator.push(context,
                              MaterialPageRoute(builder: (context) {
                            return EquipmentPoints(
                              equipment: equipments[i],
                            );
                          }));
                        }, //Todo Set ontap method
                        child: Row(
                          children: [
                            Container(
                              height: 100,
                              padding: EdgeInsets.fromLTRB(20, 7, 0, 7),
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(10)),
                              child: Column(
                                children: [
                                  Text('Name: ${equipments[i].name}'),
                                  SizedBox(height: 10),
                                  Text('Make: ${equipments[i].make}'),
                                  SizedBox(height: 10),
                                  Text('Model: ${equipments[i].model}'),
                                  SizedBox(height: 10),
                                  Text(
                                      'Year of Commissioning: ${equipments[i].commision_date}'),
                                  SizedBox(height: 10),
                                  Text(
                                      'Sump Capacity: ${equipments[i].sump_capacity}'),
                                  SizedBox(height: 10),
                                  Text('HAC code: ${equipments[i].hac_code}'),
                                  SizedBox(height: 10),
                                  Text(
                                      'Points: ${equipments[i].name}'), //Todo ask about points
                                  SizedBox(height: 10),
                                  Text(
                                      'Tickets: ${equipments[i].name}'), // Todo Make it dynamic ie if any tickets
                                  SizedBox(height: 10),
                                  Text(
                                      'Last Serviced date: ${equipments[i].name}'), //Todo findout
                                  SizedBox(height: 10),
                                  Text(
                                      'Next Planned date: ${equipments[i].name}'), //Todo findout
                                  SizedBox(height: 10),
                                ],
                              ),
                            )
                          ],
                        ),
                      ))),
        ],
      ),
    ),
  );
}

这是我的代码,我向api发出get请求以获取数据,并在我的前端显示它。我遇到了以下错误。
[错误:flutter/lib/ui/ui_dart_state.cc(209)]未处理的异常:套接字异常:操作系统错误:连接超时,错误号= 110,地址= api.generaliot.in,端口= 45700 E/flutter(26453):连接到本地套接字。启动连接(dart:io-patch/套接字修补程序。dart:681:35)E/抖动(26453):连接器连接器连接器连接器连接器连接器连接器连接器连接器#2原始套接字.开始连接(dart:io-patch/套接字_patch.dart:27:23)E/ Flutter (26453):第3个原始 ssl 。开始连接(dart:io/ ssl 。dart:237:22)E/抖动(26453):第4个 ssl 。开始连接(dart:io/ ssl 。dart:60:28)E/抖动(26453):第五个连接目标。连接(dart:http/http_impl.dart:2437:24)E/Flutter(26453):如果您有任何问题,请与我们联系。第一个问题是,如果您的浏览器中有一个客户端,那么您的浏览器将无法访问该客户端,因为您的浏览器中有一个客户端。如果您有任何问题,请与我们联系。请输入您的网址:第10个IO客户端。发送(包:http/src/io_client.dart:35:38)E/flutter(26453):(软件包:http///src/base_client.dart:93:38)E/flutter(26453):第12名BaseClient.post(软件包:http///src/base_client.dart:32:7)E/Flutter(26453):第13篇文章。( Package :http/http.dart:69:16)E/Flutter(26453):(包:http/http.dart:164:20)E/flutter(26453):15号帖子(包:http/http.dart:68:5)E/扑动(26453):(package:ncair_app/equipments.dart:133:40)(26453)使用者可以在任何时候使用任何类型的设备。
任何解决方案

js81xvg6

js81xvg61#

我用https而不是http解决了这个问题。希望这能为你节省很多时间。

相关问题