Flutter Sample app "Purchase_tester" throwing error

Badge +2
I am testing out the sample app “Purchase_tester” and getting an error in the following part of the code highlight in orange.
class _PurchaseButton extends StatelessWidget {
final Package package;
const _PurchaseButton({Key? key, required this.package}) : super(key: key);
Widget build(BuildContext context) => ElevatedButton(
onPressed: () async {
try {
final purchaserInfo = await Purchases.purchasePackage(package);
} on PlatformException catch (e) {
final errorCode = PurchasesErrorHelper.getErrorCode(e);
if (errorCode == PurchasesErrorCode.purchaseCancelledError) {
print('User cancelled');
} else if (errorCode ==
PurchasesErrorCode.purchaseNotAllowedError) {
print('User not allow to purchase');
} else if (errorCode == PurchasesErrorCode.paymentPendingError) {
print('Payment is pending');
return const InitialScreen();
child: Text('Buy - (${package.product.priceString})'),
The error says; The return type 'InitialScreen' isn't a 'Future<void>', as required by the closure's context.
Any help would be very much appreciated

2 replies

Userlevel 5
Badge +8

@William Adomako I believe the issue is that you’re returning the InitialScreen within the scope of the onPressed action of the button. 

Since onPressed is async, Dart is complaining because returning an InitialScreen isn’t async, so it’s not a Future<void>. 


It seems like you don’t need to return the InitialScreen from within the button’s onPressed action, removing that line should work (although maybe you had another use for that InitialScreen?)

Badge +2

Thank you for the feedback, I believe you have a point there, I took another approach and it worked out fine. I will try out the suggestion you have made in my next task.