Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

331
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!