flutter_paystack icon indicating copy to clipboard operation
flutter_paystack copied to clipboard

Paystack null safety version is not initializing on flutter 2

Open Codesait opened this issue 4 years ago • 12 comments

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... (package:proptor/screens/payment_methods.dart:167:48) E/flutter (23171): #5 _rootRun (dart:async/zone.dart:1346:47) E/flutter (23171): #6 _CustomZone.run (dart:async/zone.dart:1258:19) E/flutter (23171): #7 _FutureListener.handleWhenComplete (dart:async/future_impl.dart:176:18) E/flutter (23171): #8 Future._propagateToListeners.handleWhenCompleteCallback (dart:async/future_impl.dart:674:39) E/flutter (23171): #9 Future._propagateToListeners (dart:async/future_impl.dart:730:37) E/flutter (23171): #10 Future._completeWithValue (dart:async/future_impl.dart:539:5) E/flutter (23171): #11 Future._asyncCompleteWithValue. (dart:async/future_impl.dart:577:7) E/flutter (23171): #12 _rootRun (dart:async/zone.dart:1354:13) E/flutter (23171): #13 _CustomZone.run (dart:async/zone.dart:1258:19) E/flutter (23171): #14 _CustomZone.runGuarded (dart:async/zone.dart:1162:7) E/flutter (23171): #15 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1202:23) E/flutter (23171): #16 _microtaskLoop (dart:async/schedule_microtask.dart:40:21) E/flutter (23171): #17 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5) E/flutter (23171):

Codesait avatar Apr 13 '21 13:04 Codesait

Hi @Codesait can you add a snippet of how you're calling the plugin? A runnable snippet will help.

wilburx9 avatar Apr 13 '21 13:04 wilburx9

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 avatar Apr 13 '21 14:04 Codesait

@Codesait can you add a full widget that I can run? Also, include the part where you call the plugin to make payment.

wilburx9 avatar Apr 13 '21 15:04 wilburx9

@wilburt I have the same error too!

Anyitechs avatar Apr 28 '21 12:04 Anyitechs

@Anyitechs can you add a runnable snippet of how you're calling the plugin?

wilburx9 avatar Apr 28 '21 12:04 wilburx9

@wilburt Okay, give me a second.

Anyitechs avatar Apr 28 '21 12:04 Anyitechs

`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)),)],),),),`

Anyitechs avatar Apr 28 '21 12:04 Anyitechs

@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 avatar Apr 28 '21 13:04 Anyitechs

@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.

wilburx9 avatar May 30 '21 16:05 wilburx9

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 avatar Jul 26 '21 21:07 benjaminudoh10

@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.

DEVSOG12 avatar Aug 07 '21 05:08 DEVSOG12

Still getting the same error. Using flutter_paystack: ^1.0.5+1

final plugin = PaystackPlugin();

@override void initState() { plugin.initialize(publicKey: paystackkey); super.initState(); }

Screenshot 2021-12-18 at 10 07 02

Ruqoyah549 avatar Dec 18 '21 09:12 Ruqoyah549

This has been fixed. Update to the latest version and reopen this issue if the problem persists.

wilburx9 avatar Sep 19 '22 10:09 wilburx9