I have this class(dart)
class ResumenPublicaciones {
String name;
String count;
ResumenPublicaciones({this.name, this.count});
// named constructor
ResumenPublicaciones.fromJson(Map<String, dynamic> json)
: name = json['name'],
count = json['count'].toString();
}
I want to map this response from the API
[{"name":"Administración","count":37},{"name":"Call Center,
Teletrabajo y Telemarketing","count":4},{"name":"Compras,
Importaciones, Logística, Distribución","count":10}]
this is how Im doing it....
class ServicioResumenEmpleos {
List<ResumenPublicaciones> publicaciones = [];
List getResumenPublicacioness() {
publicacionesResumidas();
return publicaciones;
}
var apiUrl = "here my api URLs";
Future<ResumenPublicaciones> publicacionesResumidas() async {
var jsonResponse;
Map<String, String> headers = {
'Content-Type': 'application/json',
};
var response = await http.get(apiUrl, headers: headers);
print('respuesta del api' + response.toString());
if (response.statusCode == 200) {
print(' el API responde ' + response.body);
jsonResponse = json.decode(response.body);
var _listapublicaciones = new ResumenPublicaciones.fromJson(jsonResponse);
publicaciones.add(_listapublicaciones);
print(_listapublicaciones.name);
return _listapublicaciones;
} else {
print(
'Esta imprimiendo el else en este punto no debe impremir el response');
var _listapublicacionesNull = new ResumenPublicaciones();
_listapublicacionesNull.count = '0';
_listapublicacionesNull.name = 'didnt work';
return _listapublicacionesNull;
}
}
//
}
I want to receive a list on the class but im receiving this error msg
Exception has occurred.
**_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>')**
any Idea of what im doing wrong?
The problem is here:
var _listapublicaciones = new ResumenPublicaciones.fromJson(jsonResponse);
If your response is a list, you are passing it to a constructor that is expecting a Map. You need to iterate over the objects of your list and convert them into individual publicaciones:
var _listapublicaciones = (jsonResponse as List).map(
(o) => ResumenPublicaciones.fromJson(o),
).toList();
Related
I want to fetch data from a REST API and store the information in my local database but get errors like :
_TypeError (type '(Map<String, dynamic>) => QuizDescription' is not a subtype of type '(dynamic) => QuizDescription' of 'f')
My List model:
factory QuizDescription.fromJson(Map<String, dynamic> json) =>
QuizDescription(
quizID: json['id'],
quizname: json['quizname'],
kurzbeschreibung: json['kurzbeschreibung'],
beschreibung: json['beschreibung'],
preis: json['preis'],
quizbild: json['quizbild']['url'],
rating: json['_rating_of_quiz']['rating_rating'],
);
Map<String, dynamic> toJson() => {
"quizID": quizID,
"quizname": quizname,
"kurzbeschreibung": kurzbeschreibung,
"beschreibung": beschreibung,
"preis": preis,
"quizbild": quizbild,
"rating": rating,
};
Database-Create method where I want to store information in :
static Future<int> createQuiz(QuizDescription quiz) async {
Database db = await DBQUIZProvider.initQuizDB();
return await db.insert('quiz', quiz.toJson());
}
API Call - which is using the Database method:
var url = Uri.parse('${Constants.BASE_URL}/QUIZ/getall');
var headers = {'Content-Type': 'application/json'};
var res = await http.get(
url,
headers: headers,
);
final body = json.decode(res.body);
QuizDescription quizliste =
body.map<QuizDescription>(QuizDescription.fromJson).toList();
DBQUIZProvider.createQuiz(quizliste);
try this...
var url = Uri.parse('${Constants.BASE_URL}/QUIZ/getall');
var headers = {'Content-Type': 'application/json'};
var res = await http.get(url, headers: headers,);
List tempList = json.decode(res.body);
Database-Create method where store information in
static Future<int> createQuiz(List quiz) async {
int result = 0;
Database db = await DBQUIZProvider.initQuizDB();
for (var element in planets) {
QuizDescription quizD = QuizDescription. fromJson(element);
result = await db.insert(
'quiz',
quizD.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
return result;
}
try this:
static Future<int> createQuiz(Map<String, dynamic> row) async {
Database db = await DBQUIZProvider.initQuizDB();
return await db.insert('quiz', row);
}
I'm trying to use a CloudFlare worker to manage my backend ESI fragments but i get an error:
Uncaught (in promise) TypeError: Body has already been used. It can only be used once. Use tee() first if you need to read it twice.
Uncaught (in response) TypeError: Body has already been used. It can only be used once. Use tee() first if you need to read it twice.
I don't find where the body has already been used
The process is:
get a response with the parts
Transform the body by replacing parts fragments with sub Backend calls (streamTransformBody function)
return the response
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
});
const esiHeaders = {
"user-agent": "cloudflare"
}
async function handleRequest(request) {
// get cookies from the request
if(cookie = request.headers.get("Cookie")) {
esiHeaders["Cookie"] = cookie
console.log(cookie)
}
// Clone the request so that it's no longer immutable
newRequest = new Request(request)
// remove cookie from request
newRequest.headers.delete('Cookie')
// Add header to get <esi>
newRequest.headers.set("Surrogate-Capability", "abc=ESI/1.0")
console.log(newRequest.url);
const response = await fetch(newRequest);
let contentType = response.headers.get('content-type')
if (!contentType || !contentType.startsWith("text/")) {
return response
}
// Clone the response so that it's no longer immutable
const newResponse = new Response(response.body, response);
let { readable, writable } = new TransformStream()
streamTransformBody(newResponse.body, writable)
newResponse.headers.append('x-workers-hello', 'Hello from
Cloudflare Workers');
return newResponse;
}
async function streamTransformBody(readable, writable) {
const startTag = "<".charCodeAt(0);
const endTag = ">".charCodeAt(0);
let reader = readable.getReader();
let writer = writable.getWriter();
let templateChunks = null;
while (true) {
let { done, value } = await reader.read();
if (done) break;
while (value.byteLength > 0) {
if (templateChunks) {
let end = value.indexOf(endTag);
if (end === -1) {
templateChunks.push(value);
break;
} else {
templateChunks.push(value.subarray(0, end));
await writer.write(await translate(templateChunks));
templateChunks = null;
value = value.subarray(end + 1);
}
}
let start = value.indexOf(startTag);
if (start === -1) {
await writer.write(value);
break;
} else {
await writer.write(value.subarray(0, start));
value = value.subarray(start + 1);
templateChunks = [];
}
}
}
await writer.close();
}
async function translate(chunks) {
const decoder = new TextDecoder();
let templateKey = chunks.reduce(
(accumulator, chunk) =>
accumulator + decoder.decode(chunk, { stream: true }),
""
);
templateKey += decoder.decode();
return handleTemplate(new TextEncoder(), templateKey);
}
async function handleTemplate(encoder, templateKey) {
const linkRegex = /(esi:include.*src="(.*?)".*\/)/gm
let result = linkRegex.exec(templateKey);
let esi
if (!result) {
return encoder.encode(`<${templateKey}>`);
}
if (result[2]) {
esi = await subRequests(result[2]);
}
return encoder.encode(
`${esi}`
);
}
async function subRequests(target){
target = esiHost + target
const init = {
method: 'GET',
headers: esiHeaders
}
let response = await fetch(target, init)
if (!response.ok) {
return ''
}
let text = await response.text()
return '<!--esi-->' + text + '<!--/esi-->'
}
When I try to json decode I get the error Expected a value of type 'List', but got one of type '_JsonMap'
My code:
static Future<Response<Localizacao>> getLocalizacao(String cep) async {
await Future.delayed(Duration(milliseconds: 200));
try {
Map<String, String> headers = {
'Authorization': 'Token token=9e034db1f315356f30'};
String protocol = 'https://cors-anywhere.herokuapp.com/';
String uri =
'https://www.cepaberto.com/api/v3/cep?cep=' + cep;
final endpoint = "&format=json";
String url = protocol + uri + endpoint;
final response = await http.get(url, headers: headers);
if (response.statusCode == 200) {
final json = response.body;
List list = (jsonDecode(json) as List<dynamic>) ;
final local = list.map<Localizacao>((map) => Localizacao.fromJson(map)).toList();
return Response(true, msg: "OK", result: local[0]);
} else {
return Response(false, msg: "Erro ao conectar no web service");
}
} catch (e) {
return Response(false, msg: "Erro ao conectar no web service");
}
}
I tried other ways like:
List list = convert.json.decode(response.body);
List list = convert.json.decode(json);
Found out what the problem was.
I just had to add :
final json = "[" + response.body + "]";
Try this
if (response.statusCode == 200) {
print("IF responde==200");
final json = response.body;
print("IF rjson");
Map<String, dynamic> map= jsonDecode(json) ;
print("IF converting json");
final local = Localizacao.fromJson(map);
print("IF mapping list");
return Response(true, msg: "OK", result: local);
} else {
return Response(false, msg: "Erro ao conectar no web service");
}
} catch (e) {
print("error in getLocalizacao: $e");
return Response(false, msg: "Erro ao conectar no web service");
}
I am running into a bit of a snag setting up pagination in Google Apps Script. I am trying to use it for Shopify API. Reference links attached.
I attached the code below of what I have so far -
trying to figure out how to use the "While" statement to make it check if there is a Next Page URL
trying to figure out a way to parse the Link in the header. Example below. On pages 2+ there will be a next and previous link. We only need the next
https://shop-domain.myshopify.com/admin/api/2019-07/products.json?limit=50&page_info=eyJkaXJlY3Rpb24iOiJwcmV2IiwibGFzdF9pZCI6MTk4NTgyMTYzNCwibGFzdF92YWx1ZSI6IkFjcm9saXRoaWMgQWx1bWludW0gUGVuY2lsIn0%3D; rel="previous", https://shop-domain.myshopify.com/admin/api/2019-07/products.json?limit=50&page_info=eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6MTk4NTgzNjU0NiwibGFzdF92YWx1ZSI6IkFoaXN0b3JpY2FsIFZpbnlsIEtleWJvYXJkIn0%3D; rel="next
function callShopifyOrderCount() {
var accessToken = 'xxxx';
var store_url = 'https://xxxx.myshopify.com/admin/api/2021-01/orders.json?status=any&fields=created_at,id,name,total-price&limit=20';
var headers = {
"Content-Type": "application/json",
'X-Shopify-Access-Token': accessToken
};
var options = {
"method": "GET",
"headers": headers,
"contentType": "application/json",
"muteHttpExceptions": true,
}
var response = UrlFetchApp.fetch(store_url, options)
// Call the link header for next page
var header = response.getHeaders()
var linkHeader = header.Link;
var responseCode = response.getResponseCode();
var responseBody = response.getContentText();
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody);
var objectLength = responseJson.orders.length;
for (var i = 0; i < objectLength; i++) {
var orderId = responseJson.orders[i].id;
var orderPrice = responseJson.orders[i].total_price;
var orderName = responseJson.orders[i].name;
}
} else {
Logger.log(
Utilities.formatString(
"First Request failed. Expected 200, got %d: %s",
responseCode,
responseBody
)
);
// ...
}
// *** NEED TO FIGURE OUT WHILE STATEMENT //
while (Link != null) {
var store_url = linkHeader;
var response = UrlFetchApp.fetch(store_url, options)
var responseCode = response.getResponseCode();
var responseBody = response.getContentText();
var header = response.getHeaders()
var linkHeader = header.Link;
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody);
var objectLength = responseJson.orders.length;
for (var i = 0; i < tweetLength; i++) {
var orderId = responseJson.orders[i].id;
var orderPrice = responseJson.orders[i].total_price;
var orderName = responseJson.orders[i].name;
}
}
else {
Logger.log(
Utilities.formatString(
"Second Request failed. Expected 200, got %d: %s",
responseCode,
responseBody
)
);
}
}
}
References:
https://shopify.dev/tutorials/make-paginated-requests-to-rest-admin-api
https://www.shopify.com/partners/blog/relative-pagination
In this method, the output is as follows:
I/flutter ( 2928): 200
I/flutter ( 2928): null
I/flutter ( 2928): [Instance of 'Images']
This is causing snapshot.data to be null in my FutureBuilder as well.Any Idea why is this happening?
Future<List<Images>> getData( File f ) async {
List<Images> list;
// String link = "https://clothest.herokuapp.com/";
String link ="https://us-central1-velvety-rookery-274308.cloudfunctions.net/function-1";
var stream = new http.ByteStream(DelegatingStream.typed(f.openRead()));
var length = await f.length();
var postUri = Uri.parse(link);
var request = new http.MultipartRequest("POST", postUri);
var multipartFileSign = new http.MultipartFile('File', stream, length,
filename: basename(f.path));
request.files.add(multipartFileSign);
request.headers.addAll({"content-type": "application/json"});
var response = await request.send();
print(response.statusCode); //200 OK
if (response.statusCode == 200){
response.stream.transform(utf8.decoder).listen((value) {
var data = json.decode(value);
var rest = data["Items"] as List;
list = rest.map<Images>((json) => Images.fromJson(json)).toList();
print(list.toString());
});
}
print(list.toString());
return list;
}
So I managed to solve this problem by using another package instead of the HTTP package. Dios package is the best regarding HTTP requests. Updated code:
Future<List<Images>> getData( File f ) async {
List<Images> lst=[];
String link ="https://us-central1-velvety-rookery-274308.cloudfunctions.net/function-1";
Dio dio = new Dio();
FormData formdata = new FormData(); // just like JS
formdata.add('File', new UploadFileInfo(f, basename(f.path)));
await dio.post(link, data: formdata, options: Options(
method: 'POST',
responseType: ResponseType.JSON // or ResponseType.JSON
)).then((response) {
print(response);
if(response.statusCode==200){ //OK
var responseBody = response.data; //Map
var rest = responseBody["Items"] as List;
//print(rest);
lst = rest.map<Images>((json) => Images.fromJson(json)).toList(); //instances of 'Images' object
//print(lst);
}
}
).catchError((error) => print(error));
return lst;