Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 发生异常。FormatException(FormatException:意外字符(位于字符1处)<;HTML>;<;/HTML>;^)_Flutter_Dart - Fatal编程技术网

Flutter 发生异常。FormatException(FormatException:意外字符(位于字符1处)<;HTML>;<;/HTML>;^)

Flutter 发生异常。FormatException(FormatException:意外字符(位于字符1处)<;HTML>;<;/HTML>;^),flutter,dart,Flutter,Dart,我正在创建一个新闻应用程序,我通过JSON获取数据,在每次运行我的应用程序时,我都会在(response HTTP)上崩溃,我无法理解: 发生异常。FormatException(FormatException:意外字符(在字符1处 ^) 代码: import 'package:http/http.dart' as http; import 'dart:convert'; class Category { int id; String title; Category({

我正在创建一个新闻应用程序,我通过JSON获取数据,在每次运行我的应用程序时,我都会在(
response HTTP
)上崩溃,我无法理解:

发生异常。FormatException(FormatException:意外字符(在字符1处 ^)

代码:

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

class Category {

  int id;
  String title;

  Category({ 
    this.id,
    this.title 
  });

  static Future<List<Category>> getCategories() async {

    http.Response response = await http.get("url JSON"); // <- Here carash

    Map<String, dynamic> map = json.decode(response.body);

    List<Category> list = [];

    for(var map in map['categories']){
      list.add(
        Category(id: map['id'], title: map['title'])
      );
    }
    return list;
  }

}
import'package:http/http.dart'作为http;
导入“dart:convert”;
类别{
int-id;
字符串标题;
类别({
这个身份证,
这个名字
});
静态未来getCategories()异步{

http.Response Response=wait http.get(“url JSON”);//我认为您的请求有问题,api没有处理该错误,并像我猜的那样以html的形式抛出错误。无论如何,您解码的方式不是正确的方式

在执行http请求时,请记住几点:

  • 最重要的是研究数据类型(HTML/JSON)和 数据。通过打印到控制台或伟大的方式使用curl请求或邮递员
  • 始终确保使用
    try-catch来包装您的
    json.decode()
    块,当出现错误时,您可以正确地看到发生了什么 用你的代码
  • 确保您的响应状态代码为200。并随时处理您的数据
  • 在迭代解码响应之前,请确保它包含 价值观
  • 如果你不知道类型或者你不知道 当然,不使用静态类型来分配解码响应主体使用
    final
    (不要总是使用动态类型,我的建议仅适用于此类情况)。这将有助于您处理
    FormatException
    您所面临的情况
  • 最后但并非最不重要的一点是,当您要检查带有状态代码的错误消息时。例如, 当检查
    过期jwt
    时,您将获得
    500
    作为状态代码。 因此,在这种情况下,仅检查状态代码是不够的 因为
    500
    意味着
    内部服务器错误
例: 列表=[]

try {
    final decodedBody = json.decode(response.body); // when you have error messages with response, you can also check message with status code

    debugPrint(decodedBody?.toString());

    if (response.statusCode == 200) {
        if (decodedBody != null && map['categories'] != null) { // you can also check the data type you want here ex: (map is List)
            for(var map in map['categories']){
              list.add(Category(id: map['id'], title: map['title']));
            }

            // Below is another thrilled way to do this

            // list = decodedBody.map['categories']((item) => Category(id: item['id'], title: item['title'])).toList();
        }
    }

} catch(e, _) {
    debugPrint(e.toString());
}

希望这有帮助。按照上述步骤操作,如果有任何其他错误,请告诉我。

您收到的响应不是JSON,因此您的JSON无法解码


您的服务器返回了一些HTML标记,这意味着您可能请求了错误的URL,请在邮递员或浏览器中检查URL并查看响应。

您确定您的JSON URL返回的是JSON而不是HTML吗?