Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Parameters to Cloud Functions in Flutter

I'm Struggling to get parameters to pass into my Cloud Function when using the HttpsCallable plugin in Flutter.

The Logs in my Cloud Functions shows that no parameters were passed.

My Cloud Function index.js

// acts as an endpoint getter for Python Password Generator FastAPI
exports.getPassword = functions.https.onRequest((req, res) => {
    const useSymbols = req.query.useSymbols;
    const pwLength = req.query.pwdLength;
    // BUILD URL STRING WITH PARAMS
    const ROOT_URL = `http://34.72.115.208/password?pwd_length=${pwLength}&use_symbols=${useSymbols}`;
    const debug = {
        pwLenType: typeof pwLength,
        pwLen: pwLength,
        useSymbolsType: typeof useSymbols,
        useSymbols: useSymbols,
    };
    console.log(req.query);
    console.log(debug);
    // let password: any; // password to be received
    cors(req, res, () => {
        http.get(ROOT_URL).then((response) => {
            console.log(response.getBody());
            return res.status(200).send(response.getBody());
        })
            .catch((err) => {
            console.log(err);
            return res.status(400).send(err);
        });
        // password = https.get(ROOT_URL);
        // response.status(200).send(`password: ${password}`);
    }); // .catch((err: any) => {
    //   response.status(500).send(`error: ${err}`);
    // })
});

I made sure to Log not only the individual params I'm looking for but also the whole query.

My Dart File:

Future<String> getPassword() async {
  final HttpsCallable callable = new CloudFunctions()
      .getHttpsCallable(functionName: 'getPassword')
        ..timeout = const Duration(seconds: 30);

  try {
    final HttpsCallableResult result = await callable.call(
      <String, dynamic>{
        "pwLength": 10,
        "useSymbols": true,
      },
    );

    print(result.data['password']);
    return result.data['password'];
  } on CloudFunctionsException catch (e) {
    print('caught firebase functions exception');
    print('Code: ${e.code}\nmessage: ${e.message}\ndetails: ${e.details}');

    return '${e.details}';
  } catch (e) {
    print('caught generic exception');
    print(e);
    return 'caught generic exception\n$e';
  }
}

class DisplayPassword extends StatelessWidget {
  final String pw = (_MyPasswordGenPageState().password == null)
      ? 'error'
      : _MyPasswordGenPageState().password;

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(pw),
    );
  }
}

I would like to pass in the length of the password requested and a boolean to flag whether symbols are wanted or not. The resulting password should be displayed in an Alert Widget.

like image 566
Rafael Zasas Avatar asked Sep 15 '25 22:09

Rafael Zasas


1 Answers

Usage

import 'package:cloud_functions/cloud_functions.dart';

Getting an instance of the callable function:

final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
    functionName: 'YOUR_CALLABLE_FUNCTION_NAME',
);

Calling the function:

dynamic resp = await callable.call();

Calling the function with parameters:

dynamic resp = await callable.call(<String, dynamic>{
  'YOUR_PARAMETER_NAME': 'YOUR_PARAMETER_VALUE',
});
like image 190
Mathulan Avatar answered Sep 17 '25 12:09

Mathulan