[英]Flutter dart json Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>
[英]Dio.get - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' in Flutter/Dart
對於 Dio,我提出這樣的要求:
//Airtable (find a record)
void airtableFind() async {
try {
final response = await Dio().get(
'https://api.airtable.com/v0/'+projectBase+'/'+recordName,
queryParameters: {
// 'filterByFormula': 'SEARCH("Cactus",{Name})' // Searches the value 'Cactus' in the {'Short description'} field.
'filterByFormula': 'SEARCH('+'"'+testName+'"'+',{Name})' // Searches the value 'Cactus' in the {'Short description'} field.
// 'filterByFormula': 'SEARCH('+testName+',{Name})' // Searches the value 'Cactus' in the {'Short description'} field.
},
options: Options(
contentType: 'Application/json',
headers: {
'Authorization': 'Bearer'+' '+apiKey,
'Accept': 'Application/json',
},
),
);
// TODO: Whatever you want to do with the response. A good practice is to transform it into models and than work with them
print(response);
// var decodedString = json.decode(response.data.toString());
final decodedString = json.decode(response.data);
print(decodedString);
// print(decodedString['records'][0]['id']);
// String id = decodedString['records'][0]['id'];
} on DioError catch (e) {
// TODO: Error handling
if (e.response != null) {
// print(e.response.data);
print(e);
} else {
// print(e.request);
print(e.message);
}
}
}
這段代碼在print(response)
之前都可以正常工作。 但隨后問題就開始了。 具體來說,這是我在控制台中看到的:
I/flutter( 4289): {"records":[{"id":"recrNhzWnPdiSFaCs","createdTime":"2022-07-28T15:45:23.000Z","fields":{"Name":"Cactus ","First":"11.5","App":"The Best App","Current":"98789fg43"}}]}
E/flutter ( 4289): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
E/flutter(4289): #0 _MainWidgetState.airtableFind(package:example/main.dart:217:50)
E/flutter ( 4289): <asynchronous suspension>
那就是I/flutter ( 4289): {"records":[{"id":"recrNhzWnPdiSFaCs","createdTime":"2022-07-28T15:45:23.000Z","fields":{" Name":"Cactus","First":"11.5","App":"The Best App","Current":"98789fg43"}}]}
is print(response);
.
然后錯誤Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
。 如何解決?
以后需要從id中獲取數據,具體操作如下:
// print(decodedString['records'][0]['id']);
// String id = decodedString['records'][0]['id'];
但是目前,這些行被注釋掉了,因為錯誤發生得更早。 在更有經驗的人的建議下,我嘗試更改final decodedString = json.decode(response.data);
to var decodedString = json.decode(response.data.toString());
,但它會引發其他錯誤:
I/flutter( 4289): {"records":[{"id":"recrNhzWnPdiSFaCs","createdTime":"2022-07-28T15:45:23.000Z","fields":{"Name":"Cactus ","First":"11.5","App":"The Best App","Current":"98789fg43"}}]}
E/flutter ( 4289): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected character (at character 2)
E/flutter(4289): {records: [{id: recrNhzWnPdiSFaCs, createdTime: 2022-07-28T15:45:23.000Z, f...
E/flutter ( 4289): ^
E/flutter ( 4289):
E/flutter ( 4289): #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
E/flutter ( 4289): #1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:913:48)
E/flutter ( 4289): #2 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
E/flutter ( 4289): #3 JsonDecoder.convert (dart:convert/json.dart:612:36)
E/flutter ( 4289): #4 JsonCodec.decode (dart:convert/json.dart:216:41)
E/flutter(4289): #5 _MainWidgetState.airtableFind(package:example/main.dart:217:32)
E/flutter ( 4289): <asynchronous suspension>
問題是response.data
已經從 JSON 解碼(這是一個Map<String, dynamic>
)。 無需使用json.decode(response.data);
.
像這樣使用它:
print(response.data['records'][0]['id']);
String id = response.data['records'][0]['id'];
也可以強制response.data
為String
而不是Map
。 只需在Dio.get()
中將responseType
選項設置為ResponseType.plain
,如下所示:
final response = await Dio().get(
'https://api.airtable.com/v0/'+projectBase+'/'+recordName,
queryParameters: {'filterByFormula': 'SEARCH('+'"'+testName+'"'+',{Name})'},
options: Options(
responseType: ResponseType.plain, ////////// <- Here
contentType: 'Application/json',
headers: {
'Authorization': 'Bearer'+' '+apiKey,
'Accept': 'Application/json',
},
),
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.