5

I have been searching all over on how to implement firebase functions with a flutter application. It does not seem like there is an SDK available (yet). I've also tried adding the gradle dependency implementation 'com.google.firebase:firebase-functions:15.0.0' to my app/build.gradle but this causes build errors.

Has anyone done an implementation that works? I am unable to find any documentation on how to handle credentials and the transport of data in order to build my own firebase functions call.

I have created a rough outline of how I am thinking this is intended to work, but may be way off base.

Future<dynamic> updateProfile(String uid, AccountMasterViewModel avm) async {

    Uri uri = Uri.parse(finalizeProfileFunctionURL);
    var httpClient = new HttpClient();
    String _result = '';

    try {
      return await httpClient
        .postUrl(uri)
        .then((HttpClientRequest request) {
          return request.close();
          // authentication??
          // Fields and data??
        })
        .then((HttpClientResponse response) async {
            print(response.transform(new Utf8Codec().decoder).join());
            if (response.statusCode == HttpStatus.OK) {
              String json = await response.transform(new Utf8Codec().decoder).join();
              _result = jsonDecode(json);
              // Do some work
              return json;
            } 
            else {
              return ':\nHttp status ${response.statusCode}';
            }
          });
        }
        catch (exception) {
          return 'Failed ' + exception.toString();
        }
      }

I'd like to be able to send an object, like

{
    accountID: src.accountID, 
    accountName: src.name,
    accountImg: src.image
}

and then handle the response. But as I said, I can't find any working examples or tutorials on how to do this. It's fairly simple to do this client size and talk directly to the database, however, there are validations and data components that need to be hidden from the client, so cloud functions is the way I would like to do this.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
SeaFuzz
  • 1,177
  • 9
  • 28
  • Actually from a Flutter application perspective the HTTPS Cloud Function would simply be like a REST APIs (unless there is some Firebase authentication in the game...). E.g. you post an object to an endpoint and get an object back (or errors). So you should not get any difficulty due to the fact IT IS a Cloud Function in the back-end. You should not declare anything about the CF in your Flutter code like implementation 'com.google.firebase:firebase-functions:15.0.0 – Renaud Tarnec May 01 '18 at 00:40
  • For how to use authentication for HTTP requests see https://stackoverflow.com/questions/48477625/how-to-use-google-api-in-flutter/48485898#48485898 – Günter Zöchbauer May 08 '18 at 04:18

4 Answers4

5

Yes, there is a cloud_function package available here: https://pub.dartlang.org/packages/cloud_function.

so as to make a call to the function you can just call

CloudFunctions.instance.call(
                    functionName: 'yourCloudFunction',
                    parameters: <String, dynamic>{
                      'param1': 'this is just a test',
                      'param2': 'hi there',
                    },
                  );
ujjwal mainali
  • 379
  • 1
  • 5
  • 17
  • final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable( functionName: 'cloudFunction'); dynamic resp = await callable.call(getAllTokens); i pass an array 'getAllTokens'. can you suggest me a function so,i get all these tokens in my cloud function in index.js – Parth Bhanderi Aug 30 '19 at 06:07
3

An updated answer to calling Firebase's Cloud Functions in Flutter would be

var callable =  CloudFunctions.instance.getHttpsCallable(functionName: 'functionName'); // replace 'functionName' with the name of your function
dynamic response = callable.call(<String, dynamic>{
    'param1': param1 //replace param1 with the name of the parameter in the Cloud Function and the value you want to insert
  }).catchError((onError) {
        //Handle your error here if the function failed

  });
Gabe
  • 5,643
  • 3
  • 26
  • 54
2

This is a good tutorial on cloud functions in flutter which helped me:

https://rominirani.com/tutorial-flutter-app-powered-by-google-cloud-functions-3eab0df5f957

Mans
  • 2,953
  • 2
  • 23
  • 32
0

Cloud functions can be triggered by data change triggers in the realtime database, Firestore, or Datastore, as well as authentication triggers.

You could just persist

{
    accountID: src.accountID, 
    accountName: src.name,
    accountImg: src.image
}

to the database and register a trigger that runs a Cloud Function when data at a specific path is inserted, updated, or deleted.

https://firebase.google.com/docs/functions/firestore-events

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567