Firebase函数在Flutter中不返回响应

bttbmeg0  于 2023-05-01  发布在  Flutter
关注(0)|答案(1)|浏览(144)

我有一个firebase函数,它调用API来创建一个用户,它工作得很好。但是,我无法从Flutter应用程序中的函数中获得有效响应。无论我尝试什么,响应数据总是为null,即使函数返回的是响应,而不是错误。这段代码与文档中的示例几乎相同。有人能看出我哪里做错了吗?
功能

exports.stripeSignup = functions.https.onCall(async (data, context) => {
  const uid = context.auth.uid;
  console.log(`Received data ${JSON.stringify(data)}`);

  stripe.accounts.create({
    type: 'custom',
    business_type: 'individual',
    individual: {
      email: data.email,
      first_name: data.first_name,
      last_name: data.last_name,
    },
    country: 'GB',
    email: data.email,
    requested_capabilities: ['card_payments', 'transfers']
  })
    .then((account) => {
      console.log(`Stripe account: ${account.id}`);
      return {
        stripeId: account.id
      };
    })
    .catch((err) => {
      console.log(`Err: ${JSON.stringify(err)}`);
      return null;
    });
});
final HttpsCallable callable =
        CloudFunctions.instance.getHttpsCallable(functionName: 'stripeSignup');

    try {
      final HttpsCallableResult res = await callable.call(<String, dynamic>{
        'first_name': firstname,
        'last_name': lastname,
        'dob': dob,
        'email': email,
        'phone': phone,
      });

      print('Stripe response - ${res.data}');
      String stripeId = res.data['stripeId'];
      return stripeId;
    } on CloudFunctionsException catch (e) {
      return null;
    }
e1xvtsh3

e1xvtsh31#

我认为你应该返回你的Promises链,如下所示:

exports.stripeSignup = functions.https.onCall(async (data, context) => {
  const uid = context.auth.uid;
  console.log(`Received data ${JSON.stringify(data)}`);

  return stripe.accounts.create({    // <--  Note the return here
    type: 'custom',
    business_type: 'individual',
    individual: {
      email: data.email,
      first_name: data.first_name,
      last_name: data.last_name,
    },
    country: 'GB',
    email: data.email,
    requested_capabilities: ['card_payments', 'transfers']
  })
  .then((account) => {
    console.log(`Stripe account: ${account.id}`);
    return {
      stripeId: account.id
    };
  })
  .catch((err) => {
    // Here you should handle errors as explained in the doc
    // https://firebase.google.com/docs/functions/callable#handle_errors
    console.log(`Err: ${JSON.stringify(err)}`);
    return null;
  });
});

相关问题