Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paystack null safety version is not initializing on flutter 2 #95

Closed
Codesait opened this issue Apr 13, 2021 · 13 comments
Closed

Paystack null safety version is not initializing on flutter 2 #95

Codesait opened this issue Apr 13, 2021 · 13 comments

Comments

@Codesait
Copy link

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):

@wilburx9
Copy link
Owner

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

@Codesait
Copy link
Author

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();
}

@wilburx9
Copy link
Owner

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

@Anyitechs
Copy link

@Wilburt I have the same error too!

@wilburx9
Copy link
Owner

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

@Anyitechs
Copy link

@Wilburt Okay, give me a second.

@Anyitechs
Copy link

`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(),
'test@test.com',
_getReference());
print(accessCode);

setState(() {
isGeneratingCode = !isGeneratingCode;
});
// controller2.setState(() {});

// _closeBottomSheet2();

Charge charge = Charge()
..amount = (100 * 100).toInt()
..reference = _getReference()
..accessCode = accessCode["data"]["access_code"]
..email = 'test@test.com';
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
Copy link

Anyitechs commented Apr 28, 2021

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

@wilburx9
Copy link
Owner

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

@benjaminudoh10
Copy link

benjaminudoh10 commented Jul 26, 2021

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.

@DEVSOG12
Copy link

DEVSOG12 commented Aug 7, 2021

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

@Ruqoyah549
Copy link

Ruqoyah549 commented Dec 18, 2021

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

@wilburx9
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants