我得到SocketException OS错误,我如何修复?

2022-01-24 06:02:05 标签 apiflutterdart

我已经创建了一个世界时间应用程序使用世界时间Api早些时候,它工作绝对没问题,但几个月后,当我尝试运行我的应用程序,它给我以下错误-

Error occurred: 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";
    }
  }
}

我不知道这是什么意思,我该怎么改正呢?

任何帮助都将非常感谢

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码