我收到 SocketException OS 错误,我该如何解决?

时间:2021-06-02 15:37:49

标签: api flutter dart

我使用世界时间 Api 创建了一个世界时间应用程序,早些时候它工作得很好,但几个月后当我尝试运行我的应用程序时,它给了我以下错误-

Error occured: SocketException: OS Error: Connection reset by peer, errno = 104, address = worldtimeapi.org, port = 34034

因此,我的应用程序没有给任何时间。

这是我的主页-

import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  Map data = {};

  @override
  Widget build(BuildContext context) {
    data = data.isNotEmpty ? data : ModalRoute.of(context).settings.arguments;

    String bgImage = data['isMorning'] ? 'Day.jpeg' : 'Night.jpeg';
    Color bgColor = data['isMorning'] ? Colors.blue[100] : Colors.blueGrey[900];
    
    return Scaffold(
      backgroundColor: bgColor,
      body: SafeArea(
        child: Container(
          decoration: BoxDecoration(
              image: DecorationImage(
            image: AssetImage('assets/$bgImage'),
            fit: BoxFit.cover,
          )),
          child: Padding(
            padding: const EdgeInsets.fromLTRB(0, 120.0, 0, 0),
            child: Column(
              children: <Widget>[
                FlatButton.icon(
                  onPressed: () async {
                    dynamic output =
                        await Navigator.pushNamed(context, '/location');
                    if (output != null) {
                      setState(() {
                        data = {
                          'time': output['time'],
                          'location': output['location'],
                          'isMorning': output['isMorning'],
                          'flag': output['flag']
                        };
                      });
                    }
                  },
                  icon: Icon(
                    Icons.edit_location,
                    color: Colors.grey[300],
                  ),
                  label: Text(
                    'Edit Location',
                    style: TextStyle(
                      color: Colors.grey[300],
                    ),
                  ),
                ),
                SizedBox(height: 20.0),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      data['location'],
                      style: TextStyle(
                        fontSize: 28.0,
                        letterSpacing: 2.0,
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 20.0),
                Text(data['time'],
                    style: TextStyle(fontSize: 66.0, color: Colors.white)),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

这是我的 API 代码-

import 'dart:convert';
import 'package:http/http.dart';
import 'package:intl/intl.dart';

class WorldTime {
  String location;
  String time;
  String flag;
  String url;
  bool isMorning = false;

  WorldTime({this.location, this.flag, this.url});

  Future<void> getTime() async {
    try {
      print('Getting Response...');
      Response response =
          await get(Uri.https('worldtimeapi.org', 'api/timezone/$url'));
      print(response.statusCode);
      if (response.statusCode == 200) {
        Map data = jsonDecode(response.body);

        String datetime = data['datetime'];
        String offset = data['utc_offset'].substring(0, 3);
        String offset_mnt = data['utc_offset'].substring(4, 6);

        DateTime now = DateTime.parse(datetime);
        now = now.add(
            Duration(hours: int.parse(offset), minutes: int.parse(offset_mnt)));
        isMorning = now.hour > 6 && now.hour < 20 ? true : false;

        time = DateFormat.jm().format(now);
      } else {
        throw Exception('Error at the moment');
      }
    } catch (e) {
      print("Error occured: $e");
      time = "Cannot Display Time Due to Error Occured";
    }
  }
}

我不知道这是什么意思,我该如何纠正?

任何帮助将不胜感激

0 个答案:

没有答案