Khalifa Alkhatri
Khalifa Alkhatri

Reputation: 294

Flutter firebase function error : Response is not valid JSON object

Hello I tried to use firebase function by using Cloud_Functions Pkg but I got error in flutter consel , I tried to pass parameters in function which is UID of user .

Consel Error :

E/flutter (17871): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: [firebase_functions/internal] Response is not valid JSON object.
E/flutter (17871): #0      catchPlatformException
package:cloud_functions_platform_interface/…/utils/exception.dart:21
E/flutter (17871): #1      _rootRunBinary (dart:async/zone.dart:1378:47)
E/flutter (17871): #2      _CustomZone.runBinary (dart:async/zone.dart:1272:19)
E/flutter (17871): #3      _FutureListener.handleError (dart:async/future_impl.dart:166:20)
E/flutter (17871): #4      Future._propagateToListeners.handleError (dart:async/future_impl.dart:716:47)
E/flutter (17871): #5      Future._propagateToListeners (dart:async/future_impl.dart:737:24)
E/flutter (17871): #6      Future._completeError (dart:async/future_impl.dart:547:5)
E/flutter (17871): #7      _completeOnAsyncError (dart:async-patch/async_patch.dart:264:13)
E/flutter (17871): #8      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart)
package:flutter/…/services/platform_channel.dart:1
E/flutter (17871): <asynchronous suspension>

Firebse Function :

exports.helloWorld = functions.https.onCall((data, context) => {
  return data.data()['uid'];
});

Flutter run function from Firebase :

    IconButton(
        icon: Icon(Icons.add),
        onPressed: () async {
          HttpsCallable callable =
              FirebaseFunctions.instance.httpsCallable('listFruit');
          final results = await callable.call(<String, dynamic>{
            'uid': '123',
          });
          print(results
              .data.toString()); // ["Apple", "Banana", "Cherry", "Date", "Fig", "Grapes"]
        });

My goal :

pass parameters to firebase function .

Upvotes: 4

Views: 1849

Answers (2)

Naaooj
Naaooj

Reputation: 910

I had the same error and it was due to the fact that the region was not provided, which seems to be required if the function is not deployed in us-central1. Following their documentation, you can perform the call like this:

FirebaseFunctions.instanceFor(region: 'europe-west1').httpsCallable('listFruit');

Upvotes: 6

Renaud Tarnec
Renaud Tarnec

Reputation: 83093

Instead of

exports.helloWorld = functions.https.onCall((data, context) => {
  return data.data()['uid'];
});

you should do

exports.helloWorld = functions.https.onCall((data, context) => {
  return data['uid'];  // Or  data.uid
});

Few more details in the Callable Cloud Functions doc.


In addition, note that your Cloud Function is named helloWorld but you call it with FirebaseFunctions.instance.httpsCallable('listFruit');. So you should adapt one or the other, e.g. FirebaseFunctions.instance.httpsCallable('helloWorld');

Upvotes: 2

Related Questions