我的应用在 iOS 和 Android 上都运行良好,但在更新到 Flutter 2.10.0 后我遇到了这个问题:
Unhandled Exception: Null check operator used on a null value
E/flutter ( 8991): #0 Element.widget (package:flutter/src/widgets/framework.dart:3203:31)
E/flutter ( 8991): #1 Provider._inheritedElementOf (package:provider/src/provider.dart:341:17)
E/flutter ( 8991): #2 Provider.of (package:provider/src/provider.dart:293:30)
E/flutter ( 8991): #3 Shop.getPastPurchases.<anonymous closure> (package:my_rents/services/shop.dart:123:20)
E/flutter ( 8991): #4 _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter ( 8991): #5 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 8991): #6 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter ( 8991): #7 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
E/flutter ( 8991): #8 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
E/flutter ( 8991): #9 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11)
E/flutter ( 8991): #10 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:663:7)
E/flutter ( 8991): #11 _rootRun (dart:async/zone.dart:1418:47)
E/flutter ( 8991): #12 _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8991): #13 _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8991): #14 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8991): #15 _rootRun (dart:async/zone.dart:1426:13)
E/flutter ( 8991): #16 _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 8991): #17 _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 8991): #18 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 8991): #19 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter ( 8991): #20 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
我的实现:
Future<List<PurchaseDetails>> getPastPurchases(BuildContext context) async {
if (this.purchases.isEmpty) {
final Stream<List<PurchaseDetails>> purchaseUpdated =
inAppPurchase.purchaseStream;
_subscription = purchaseUpdated.listen((purchaseDetailsList) {
if (purchaseDetailsList.isEmpty) {
Provider.of<AdState>(context, listen: false).toggleAds(context, true);
} else {
listenToPurchaseUpdated(context, purchaseDetailsList);
}
}, onDone: () {
_subscription.cancel();
}, onError: (error) {
});
await inAppPurchase.restorePurchases(); // If I comment this line everything works
}
return this.purchases;
}
同样,我没有对我的代码进行任何更改,只是更新了 Flutter