【发布时间】:2021-06-18 12:18:29
【问题描述】:
当我在邮递员中运行 google book API 时,它工作正常并给出状态码 200。但在颤振中,它给出了以下错误。并给出 404 状态码。 我已经生成了 API 密钥,还启用了 Google Book API。 所以如果有人知道我做错了什么,请帮助我。
TestLayout.dart
class TestLayout extends StatefulWidget {
static String id = "Test_Layout";
@override
_TestLayoutState createState() => _TestLayoutState();
}
class _TestLayoutState extends State<TestLayout> {
Future<void> getData() async {
String ApiKey = "..........";
final response = await http.get(
Uri.https("books.googleapis.com",
"books/v1/volumes?q=flowers+inauthor:keyes&key=$ApiKey"),
headers: {"Content-Type": "application/json"});
var jsonBody = jsonDecode(response.body);
//book b1=book.fromJson(jsonBody);
print(response.statusCode);
print(b1.id);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter"),
),
body: Column(
children: [
TextButton(
onPressed: () async {
await getData();
},
child: Text("Submit")),
],
),
);
}
}
Book.dart:
class book {
final int id;
book({this.id});
factory book.fromJson(Map<String,dynamic> json)
{
return book(
id: json["totalItems"]
);
}
}
错误:
E/flutter (27854): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception:
FormatException: Unexpected character (at character 1)
E/flutter (27854): <!DOCTYPE html>
E/flutter (27854): ^
E/flutter (27854):
E/flutter (27854): #0 _ChunkedJsonParser.fail (dart:convert-
patch/convert_patch.dart:1404:5)
E/flutter (27854): #1 _ChunkedJsonParser.parseNumber (dart:convert-
patch/convert_patch.dart:1271:9)
E/flutter (27854): #2 _ChunkedJsonParser.parse (dart:convert-
patch/convert_patch.dart:936:22)
E/flutter (27854): #3 _parseJson (dart:convert-patch/convert_patch.dart:40:10)
E/flutter (27854): #4 JsonDecoder.convert (dart:convert/json.dart:506:36)
E/flutter (27854): #5 JsonCodec.decode (dart:convert/json.dart:157:41)
E/flutter (27854): #6 jsonDecode (dart:convert/json.dart:96:10)
E/flutter (27854): #7 _TestLayoutState.getData
(package:freebookshare/TestFile/TestLayout.dart:22:20)
E/flutter (27854): <asynchronous suspension>
E/flutter (27854): #8 _TestLayoutState.build.<anonymous closure>
(package:freebookshare/TestFile/TestLayout.dart:40:17)
E/flutter (27854): <asynchronous suspension>
E/flutter (27854):
【问题讨论】: