在我升级flutter版本之前,一切都很顺利,没有任何警告。但是在我升级flutter之后,突然出现了一个警告。不要在异步间隙中使用'BuildContext'。尝试重写代码,不要引用'BuildContext'。
问题就在这里
ProfileMenuItem(
iconUrl: 'assets/ic_edit_profile.png',
title: 'Edit Profile',
onTap: () async {
if (await Navigator.pushNamed(context, '/pin') == true) {
if(context.mounted) Navigator.pushNamed(context, '/profile-edit');
}
},
),
下面是我的完整代码
import 'package:bank_sha/shared/theme.dart';
import 'package:bank_sha/ui/widgets/buttons.dart';
import 'package:bank_sha/ui/widgets/profile_menu_item.dart';
import 'package:flutter/material.dart';
class ProfilePage extends StatelessWidget {
const ProfilePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('My Profile'),
),
body: ListView(
padding: const EdgeInsets.symmetric(
horizontal: 24,
),
children: [
const SizedBox(
height: 20,
),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 30,
vertical: 22,
),
decoration: BoxDecoration(
color: whiteColor,
borderRadius: BorderRadius.circular(20),
),
child: Column(
children: [
Container(
height: 120,
width: 120,
decoration: const BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage('assets/img_profile.png'),
),
),
child: Align(
alignment: Alignment.topRight,
child: Container(
width: 28,
height: 28,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: whiteColor,
),
child: Center(
child: Icon(
Icons.check_circle,
color: greenColor,
size: 24,
),
),
),
),
),
const SizedBox(
height: 16,
),
Text(
'Mr Geblekx',
style: blackTextStyle.copyWith(
fontSize: 18,
fontWeight: medium,
),
),
const SizedBox(
height: 40,
),
ProfileMenuItem(
iconUrl: 'assets/ic_edit_profile.png',
title: 'Edit Profile',
onTap: () async {
if (await Navigator.pushNamed(context, '/pin') == true) {
if(context.mounted) Navigator.pushNamed(context, '/profile-edit');
}
},
),
ProfileMenuItem(
iconUrl: 'assets/ic_pin.png',
title: 'My Pin',
onTap: () async {
if (await Navigator.pushNamed(context, '/pin') == true) {
if(context.mounted) Navigator.pushNamed(context, '/profile-edit-pin');
}
},
),
ProfileMenuItem(
iconUrl: 'assets/ic_wallet.png',
title: 'Wallet Settings',
onTap: () {},
),
ProfileMenuItem(
iconUrl: 'assets/ic_reward.png',
title: 'My Rewards',
onTap: () {},
),
ProfileMenuItem(
iconUrl: 'assets/ic_help.png',
title: 'Help Center',
onTap: () {},
),
ProfileMenuItem(
iconUrl: 'assets/ic_logout.png',
title: 'Log Out',
onTap: () {},
),
],
),
),
const SizedBox(
height: 87,
),
CustomTextButton(
title: 'Report a Problem',
onPressed: () {},
),
const SizedBox(
height: 50,
),
],
),
);
}
}
我试图添加上下文。安装在等待,但它不工作,有人能帮助我吗?谢谢
2条答案
按热度按时间gj3fmq9x1#
将
ProfilePage
转换为StatefulWidget
,并使用if(mounted)
检查小工具是否仍处于挂载状态。v7pvogib2#
试着这样做: