I get this error: Undefined name ‘mounted’ in the future fetchoffers widget. Can anyone help me with this problem? I want to use this to make in app purchases work in my flutter app.
import 'package:flutter/material.dart';
import 'package:AnyKnower/chatgpt.dart';
import 'package:AnyKnower/privacypolicy.dart';
import 'package:AnyKnower/purchase_api.dart';
import 'package:AnyKnower/utils.dart';
import 'package:glassfy_flutter/models.dart';
import 'package:provider/provider.dart';
import 'chat_screen.dart';
import 'package:page_transition/page_transition.dart';
import 'glassfy_provider.dart';
class NavBar extends StatelessWidget {
bool isSubscribed = false;
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
ClipRRect(
borderRadius: BorderRadius.only(
topRight: Radius.circular(15),
),
child: UserAccountsDrawerHeader(
accountName: Text('AnyKnower'),
accountEmail: Text(''),
currentAccountPicture: CircleAvatar(
child: ClipOval(
child: Image.asset('assets/logo4.jpg', width: 100, height: 100, fit: BoxFit.cover),
),
),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/background1.jpg"),
fit: BoxFit.cover,
),
),
),
),
ListTile(
leading: Icon(Icons.chat_rounded,color: Colors.blueGrey,),
title: Text("AnyKnower Chat"),
onTap: () {
Navigator.of(context).push(PageTransition(child: ChatScreen(), type: PageTransitionType.fade));
}
),
ListTile(
leading: Icon(Icons.shopping_cart_outlined,color: Colors.blueGrey,),
title: Text("The AnyKnower Shop"),
onTap: () {
fetchOffers(context);
}
),
ListTile(
leading: Icon(Icons.chat_outlined,color: Colors.blueGrey,),
title: Text("Beta Premium chat"),
onTap: () {
Navigator.of(context).push(PageTransition(child: chatgpt(), type: PageTransitionType.fade));
}
),
ListTile(
leading: Icon(Icons.privacy_tip_outlined,color: Colors.blueGrey,),
title: Text("Privacy Policy"),
onTap: () {
Navigator.of(context).push(PageTransition(child: privacypolicy(), type: PageTransitionType.fade));
}
),
],
)
);
}
Future fetchOffers(BuildContext context) async {
final offerings = await PurchaseApi.fetchOffers();
final offer = offerings.singleWhere((offering) => offering.offeringId == '100AnyKnowerCredit');
if(!mounted) return;
Utils.showSheet(
context,
(context) => PayWallWidget(
title: 'Upgrade your plan',
description: 'kkdkdd',
offer: offer,
onClickedSku: (sku) async {
final transaction = await PurchaseApi.purchaseSku(sku);
if (mounted!) return;
if (transaction != null) {
final provider = context.read<GlassfyProvider>();
provider.add10Coins();
}
Navigator.pop(context);
},
)
);
}
}
Do i maybe need to import something? I saw by an other post that it needed to be an statefullwidget but what do i need to change to make that happen then?
2
Answers
Thank You Yeasin Sheikh for the right solution.
My solution is to use
if (!context.mounted) return;
instead ofif(!mounted) return;
Since Flutter version 3.7.0 BuildContext now has a mounted property. Here’s the PR https://github.com/flutter/flutter/pull/111619.