With Dio, I make a request like this:
//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);
}
}
}
This code works fine up until print(response)
. But then the problems begin. Specifically, this is what I see in the console:
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>
That is 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);
.
And then the error Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
. How to fix it?
In the future, I need to get data from id, which I will do as follows:
// print(decodedString['records'][0]['id']);
// String id = decodedString['records'][0]['id'];
But at the moment, these lines are commented out, because the error occurs earlier. On the advice of more experienced people, I tried to change final decodedString = json.decode(response.data);
to var decodedString = json.decode(response.data.toString());
, but it throws other errors:
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>
CodePudding user response:
The problem is that response.data
is already decoded from JSON (It's a Map<String, dynamic>
). No need to decode it again with json.decode(response.data);
.
Use it like this:
print(response.data['records'][0]['id']);
String id = response.data['records'][0]['id'];
It's also possible to force response.data
to be a String
instead of a Map
. Just set the responseType
option to ResponseType.plain
in the Dio.get()
like this:
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',
},
),
);