异常:FormatException:意外字符(在字符1处) [英] Exception: FormatException: Unexpected character (at character 1)

查看:134
本文介绍了异常:FormatException:意外字符(在字符1处)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是不熟悉Flutter的人,而是通过有关Flutter的教程学习的,所以我遵循相同的代码,并且已经尝试重写几乎所有内容,但是当我开始运行该应用程序时,却遇到了上面的错误.

I'm new to flutter and learning through a tutorial about Flutter So I was following the same codes and already tried rewriting almost everything but when I started to run the app and i got that above error.

错误:flutter/lib/ui/ui_dart_state.cc(157)

ERROR:flutter/lib/ui/ui_dart_state.cc(157)

异常:FormatException:意外字符(在字符1处)E/flutter(14218):

我的代码:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart';


class Loading extends StatefulWidget {
  @override
  _LoadingState createState() => _LoadingState();
}

class _LoadingState extends State<Loading> {

  void getTime() async {
   Response response= await get('http://worldtimeapi.org/timezone/Europe/London');
    Map data=jsonDecode(response.body);
    print(data);


  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getTime();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(
        backgroundColor: Colors.blue[800],
        title: Text('here is loading'),
      ),
      body: Text("choose location screen"),
    );
  }
}

推荐答案

我认为您的意思是此链接:

I think you mean this link: https://worldtimeapi.org/api/timezone/Europe/London.

不是这个:

Response response= await get('http://worldtimeapi.org/timezone/Europe/London');

我上面给您的链接为您提供了一个JSON响应,您可以在其中对应用程序进行解码.您正在链接到一个没有给您JSON响应的实际网页,这就是您收到该错误的原因.我也将其设置为HTTPS,因此在您的应用中更加安全.

The link I gave you above gives you a JSON response in which you decode in your app. You were linking to an actual webpage that doesn't give you a JSON response, that's why you got that error. I have also made it HTTPS so it's more secure in your app.

这篇关于异常:FormatException:意外字符(在字符1处)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆