Search code examples
firebasefluttergoogle-cloud-functionsflutter-web

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.


Solution

  • Your client code is trying to invoke a callable function, but your function is different type of HTTP function. They are not compatible - be sure to read the documentation to understand how they differ.

    If you want to use your client code to invoke a funcition, you will need to follow the instructions for callable functions, and use onCall to declare it, not onRequest.