Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

334
Visualizações
Flutter - Return Null value while using Http Request in Cloud Functions

Here is my issue, I have a callable Cloud Function I would like to use on my Flutter app.

You can find the code below.

However, each time I press the button corresponding to the function I always have a "null" value on my app coming from Cloud Functions even if my answer is printed into the console.

From what I understand, the function return a "null" value as the jsonResponse is not here yet.

I would like to find a way to return the jsonResponse only when the answer is there.

I tried async, await, and promises already, but maybe I used it the wrong way ?

Thank you


// code on JS

exports.writeMessage = functions.https.onCall(async (data, context) => {

  global.XMLHttpRequest = require("xhr2");
  function makeRequest(method, url) {
    appleMusicKey = 'XXXXXXX'
    return new Promise(function (resolve, reject) {
      let http = new XMLHttpRequest();
      http.open(method, url);
      http.setRequestHeader('Authorization', 'Bearer ' + appleMusicKey);
      http.onload = function () {
        if (this.status >= 200 && this.status < 300) {
          resolve(JSON.parse(http.response)['data'][0]['attributes']['url'].toString());
        } else {
          reject({
            status: this.status,
            statusText: http.statusText
          });
        }
      };
      http.onerror = function () {
        reject({
          status: this.status,
          statusText: xhr.statusText
        });
      };
      http.send();
    });
  }

  async function doAjaxThings() {
    var isrcCode = 'QMBZ92168114';
    let result = await makeRequest("GET", `https://api.music.apple.com/v1/catalog/fr/songs?filter[isrc]=${isrcCode}`);
    
    console.log(result);

    return new Promise(function (resolve, reject) {
      resolve(result);
      reject('error');
    });
  }

  doAjaxThings();

});

// code on Flutter for calling the function

  Future<void> writeMessage(
  ) async {
    HttpsCallable callable =
        FirebaseFunctions.instance.httpsCallable('writeMessage');
    final resp = await callable.call();
    print(resp.data);
  }
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda