我在firebase上部署了一个HTTPS函数,并试图从Flutter应用程序中调用它,但什么也没发生。
我的代码中出现以下错误:
未引用声明"paymentRequest"。请尝试删除"paymentRequest"的声明。
以下是我在Firebase中成功部署的功能:
// Generate Payment URL
exports.paymentRequest = functions.https.onCall(async (data, context) => {
const clientAccessToken = data.clientAccessToken;
const recipientIban = data.recipientIban;
const recipientName = data.recipientName;
const paymentDescription = data.paymentDescription;
const paymentReference = data.paymentReference;
const productPrice = parseInt(data.productPrice);
const jsonData = {
"destinations": [
{
"accountNumber": recipientIban,
"type": "iban"
}
],
"amount": productPrice,
"currency": "EUR",
"market": "FR",
"recipientName": recipientName,
"sourceMessage": paymentDescription,
"remittanceInformation": {
"type": "UNSTRUCTURED",
"value": paymentReference
},
"paymentScheme": "SEPA_INSTANT_CREDIT_TRANSFER"
};
axios({
method: "post",
url: "https://myapiendpoint.com/api/v1/requests",
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${clientAccessToken}`,},
data: jsonData,
})
.then(function (response) {
//handle success
console.log(response.data);
})
.catch(function (response) {
//handle error
console.log(response);
});
return response.data;
})
这是我的颤振代码的摘录:
import 'package:cloud_functions/cloud_functions.dart';
...visual stuff...
child: ElevatedButton(
child: const Text('Submit'),
onPressed: () {
Future<void> paymentRequest(String message) async { // This is where I have an error
HttpsCallable callable = FirebaseFunctions.instance
.httpsCallable('paymentRequest');
final resp = await callable.call(
<String, dynamic>{
'clientAccessToken':
'a_valid_token',
'recipientIban': ibanController,
'recipientName': nameController,
'paymentDescription': descriptionController,
'paymentReference': referenceController,
'productPrice': productPriceController
},
);
print("result: ${resp.data}");
}
showDialog(
context: context,
builder: (context) {
return AlertDialog(
// Retrieve the text the that user has entered by using the
// TextEditingController.
title: Text('Success!'),
// content: Text(productPriceController.text),
content: Text(
'The product page has been successfully updated.'),
actions: [
Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(),
onPressed: () async {
// Navigator.pushReplacementNamed(
// context, '/');
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => HomeScreen(
productPrice:
productPriceController
.text)));
},
child: Text('Go to Product page'),
),
),
],
);
},
);
},
)
有什么想法吗?
谢谢!
而不是在回调中声明一个新函数
onTap: () {
Future<void> someFunctionDeclaration() async {
// your cloud functions call and consequent showDialog
}
}
只需在小部件(或其他文件(中创建一个单独的函数
class X extends StatelessWidget {
...
@override
Widget build(BuildContext context) {
return Widget(
// visual stuff
ElevatedButton(
onTap: () async {
await someFunction(messageVariable);
showDialog...
}
),
// more visual stuff
// end of build method
}
Future<void> someFunction(String message) async {
HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('paymentRequest');
final resp = await callable.call(
<String, dynamic>{
'clientAccessToken': 'a_valid_token',
'recipientIban': ibanController,
'recipientName': nameController,
'paymentDescription': descriptionController,
'paymentReference': referenceController,
'productPrice': productPriceController
},
);
}
}
这也有使代码更干净的优点。在view
代码中读取类似processPayment(data)
的内容比读取整个业务逻辑段更容易。