Open crobertson247 opened 9 months ago
👀 We've just linked this issue to our internal tracker and notified the team. Thank you for reporting, we're checking this out!
Thanks for the report!
That error happens if you try to use Purchases
before initializing it.
In your initialization code it looks like you have many calls inside of a try
. If any of the calls prior to PurchaseApi.init();
throw an exception, you're simply logging the error and moving on, which means that Purchases
will end up being uninitialized.
I recommend separating each of those initializations so you can isolate what's throwing an exception, and so you don't inadvertently leave Purchases
uninitialized.
I added the try catch as a suggestion from someone on the flutter form, however, before it I didn't use the try catch and didn't get any errors on the initialization of the app. I'm able to access all the subscription information up until i close and reopen the app, I noticed i was using a static variable to try and not call configure multiple times, but even after changing it to shared preferences i still get the same error.
I call the init method multiple times to ensure it is initialised before using.
static bool isConfigured = false;
static Future init() async {
try {
PreferencesService preferencesService = PreferencesService();
isConfigured = await preferencesService.getConfigurationStatus();
await Purchases.setLogLevel(LogLevel.debug);
if (isConfigured == false) {
await Purchases.configure(
PurchasesConfiguration(StoreConfig.instance.apiKey));
isConfigured = true;
await preferencesService.setConfigurationStatus(true);
}
} catch (e) {
print(e);
}
}
This is the error log i get now:
Thread 0 name:
Thread 0 Crashed:
0 libswiftCore.dylib 0x00000001847363fc _assertionFailure(_:_:file:line:flags:) + 264 (AssertCommon.swift:144)
1 PurchasesHybridCommon 0x0000000106477d0c 0x106464000 + 81164
2 PurchasesHybridCommon 0x000000010646f3b4 0x106464000 + 46004
3 PurchasesHybridCommon 0x000000010646b980 0x106464000 + 31104
4 purchases_flutter 0x00000001077f9f2c 0x1077f0000 + 40748
5 purchases_flutter 0x00000001077f893c 0x1077f0000 + 35132
6 Flutter 0x0000000107f2596c 0x107944000 + 6166892
7 Flutter 0x0000000107987c00 0x107944000 + 277504
8 libdispatch.dylib 0x00000001933106a8 _dispatch_call_block_and_release + 32 (init.c:1530)
9 libdispatch.dylib 0x0000000193312300 _dispatch_client_callout + 20 (object.m:561)
10 libdispatch.dylib 0x0000000193320998 _dispatch_main_queue_drain + 984 (queue.c:7813)
11 libdispatch.dylib 0x00000001933205b0 _dispatch_main_queue_callback_4CF + 44 (queue.c:7973)
12 CoreFoundation 0x000000018b34cf9c __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16 (CFRunLoop.c:1780)
13 CoreFoundation 0x000000018b349ca8 __CFRunLoopRun + 1996 (CFRunLoop.c:3149)
14 CoreFoundation 0x000000018b3493f8 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420)
15 GraphicsServices 0x00000001ce8d74f8 GSEventRunModal + 164 (GSEvent.c:2196)
16 UIKitCore 0x000000018d76f8a0 -[UIApplication _run] + 888 (UIApplication.m:3685)
17 UIKitCore 0x000000018d76eedc UIApplicationMain + 340 (UIApplication.m:5270)
18 Runner 0x0000000104dd077c 0x104dc8000 + 34684
19 dyld 0x00000001ae09edcc start + 2240 (dyldMain.cpp:1269)
It looks like the stacktrace doesn't have symbols, so it's hard to know what's going on, although it looks pretty much the same as the original.
As a suggestion that I think might help you figure this out. There's a isConfigured
property (https://pub.dev/documentation/purchases_flutter/latest/purchases_flutter/Purchases/isConfigured.html) you can use instead of the code you shared that uses PreferencesService. That function will check if there's an instance of Purchases, and your crash means there's no instance, so checking isConfigured
before interacting with the SDK will prevent the exception.
When I open the app everything works fine but when i close it and reopen it from the home screen it crashes on TestFlight.
Here's the crash log of the thread its crashing on:
this is my main.dart code: