flutter_paystack
flutter_paystack copied to clipboard
Paystack null safety version is not initializing on flutter 2
E/flutter (23171): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Paystack SDK has not been initialized. The SDK has to be initialized before use
E/flutter (23171): #0 PaystackPlugin._validateSdkInitialized (package:flutter_paystack/src/common/paystack.dart:143:7)
E/flutter (23171): #1 PaystackPlugin.publicKey (package:flutter_paystack/src/common/paystack.dart:61:5)
E/flutter (23171): #2 PaystackPlugin.checkout (package:flutter_paystack/src/common/paystack.dart:130:22)
E/flutter (23171): #3 payStackPay (package:proptor/payment_methods/payStack.dart:41:44)
E/flutter (23171): #4 _PaymentMethodsState.build.
Hi @Codesait can you add a snippet of how you're calling the plugin? A runnable snippet will help.
Hi @Codesait can you add a snippet of how you're calling the plugin? A runnable snippet will help.
final plugin = PaystackPlugin();
@override void initState() { plugin.initialize(publicKey: paystackTestKEy); super.initState(); }
@Codesait can you add a full widget that I can run? Also, include the part where you call the plugin to make payment.
@wilburt I have the same error too!
@Anyitechs can you add a runnable snippet of how you're calling the plugin?
@wilburt Okay, give me a second.
`import 'package:flutter_paystack/flutter_paystack.dart';
...........///
final GlobalKey scaffoldKey = new GlobalKey();
@override void initState() { PaystackPlugin.initialize(publicKey: publicKey); super.initState(); }
String getReference() { String platform; if (Platform.isIOS) { platform = 'iOS'; } else { platform = 'Android'; } return 'ChargedFrom${platform}${DateTime.now().millisecondsSinceEpoch}'; }
///paystack payment chargeCard() async { setState(() { isGeneratingCode = !isGeneratingCode; }); // controller2.setState(() {});
Map accessCode = await createAccessCode( "[Secret key here]", (100 * 100).toInt(), '[email protected]', _getReference()); print(accessCode);
setState(() { isGeneratingCode = !isGeneratingCode; }); // controller2.setState(() {});
// _closeBottomSheet2();
Charge charge = Charge() ..amount = (100 * 100).toInt() ..reference = _getReference() ..accessCode = accessCode["data"]["access_code"] ..email = '[email protected]'; CheckoutResponse response = await PaystackPlugin.checkout( context, logo: Image(image: AssetImage(imageHere),), method: CheckoutMethod.selectable, // _method, // Defaults to CheckoutMethod.selectable charge: charge, ); if (response.status == true) { print('Ref =' + response.reference.toString()); print('Checking Transaction Status'); setState(() { isLoading = true; }); try { var respons = await Network() .getPayStack(response.reference.toString()) .timeout(const Duration(seconds: 10));
if (respons.statusCode == 200) {
print('Success');
// _upload();
//todo handle payment file upload here
// _detailsUpload();
} else {
setState(() {
isLoading = false;
});
_showAlertDialogue();
print(respons.stream);
}
} on TimeoutException catch () { print('timeout exception occured' + .toString()); setState(() { isLoading = false; }); print(.message); } on SocketException catch () { print('Socket exception occured' + .toString()); setState(() { isLoading = false; }); print(.message); } on Exception catch () { setState(() { isLoading = false; }); print(.toString()); } } else { setState(() { isGeneratingCode = isGeneratingCode; }); // controller2.setState(() {}); Fluttertoast.showToast(msg: response.message); } }
@override Widget build(BuildContext context) { return Scaffold( key: scaffoldKey, /.........../
GestureDetector( onTap: () => { chargeCard() }, child: Container( margin: EdgeInsets.only(top: 35.0), width: 335.0, height: 45.0, decoration: BoxDecoration( borderRadius: BorderRadius.circular(5.0), color: Color.fromRGBO(238, 238, 238, 0.8) ), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Image(image: AssetImage('images/paystack_logo.png'),), SizedBox(width: 25.0,), isGeneratingCode ? CircularProgressIndicator(value: 1.0,) : Text( 'Paystack', style: TextStyle( fontSize: 14.0, fontFamily: 'Poppins', fontWeight: FontWeight.w500, color: Color(0xff333333)),)],),),),`
@wilburt Please note that I'm using the "1.0.4+1" version here, because the latest version is conflicting with the rave_flutter SDK you built too. I need the two payment integration on the app I'm working on so I'm using the two SDKs. The latest version of flutter_paystack worked fine with flutter 2.0, but I keep getting this error when I switched to this version "1.0.4+1".
It will be nice if you can update the rave_flutter SDK as so many of it's packages are no longer compatible with flutter 2.0. I had to upgrade the flutter_svg dependency version on my local copy of the SDK to get it to work even after I've downgraded some of my packages. It will be nice if you can look at that too. Thanks.
@Anyitechs I will advise you to bump flutter_paystack to the latest version and override the troublesome dependency as I am waiting on johnpryan/tripledes-dart#14 to support Flutter 2 for rave_flutter.
I had to look deep into the package code before I concluded that the error was not from the package.
What worked for me is to put an await before the plugin.initialize(...).
@Codesait
final plugin = PaystackPlugin();
@override
void initState() async {
await plugin.initialize(publicKey: paystackTestKEy);
super.initState();
}
@wilburt I think this detail should be added in the documentation on pub.dev and the Github README.md file.
@benjaminudoh10 It is not possible to await in initState() it should not be asynchronous. Having await means that the Widget Build is delayed.
I tried this and it worked:
final plugin = PaystackPlugin();
@override
void initState() {
plugin.initialize(publicKey: paystackkey);
super.initState();
}
Make sure you always call the same instance of the plugin.
Incase you need to run an await in the initState() you can create an async function and run the function.
Still getting the same error. Using flutter_paystack: ^1.0.5+1
final plugin = PaystackPlugin();
@override void initState() { plugin.initialize(publicKey: paystackkey); super.initState(); }
This has been fixed. Update to the latest version and reopen this issue if the problem persists.