dart 更新后,我得到消息:不要跨异步间隙使用“BuildContext”,尝试重写代码,使其不引用“BuildContext”

jk9hmnmh  于 2023-03-21  发布在  其他
关注(0)|答案(2)|浏览(117)

在我升级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,
          ),
        ],
      ),
    );
  }
}

我试图添加上下文。安装在等待,但它不工作,有人能帮助我吗?谢谢

gj3fmq9x

gj3fmq9x1#

ProfilePage转换为StatefulWidget,并使用if(mounted)检查小工具是否仍处于挂载状态。

v7pvogib

v7pvogib2#

试着这样做:

onTap: () async {
  // Declare navigator instance (or other context using methods/classes)
  // before async method is called to use it later in code
  final navigator = Navigator.of(context);

  // Now use the navigator without the warning
  if (await navigator.pushNamed('/pin') == true) {
    navigator.pushNamed('/profile-edit');
  }
},

相关问题