提问者:小点点

处理手势时抛出以下TypeErrorImpl:意外的空值


当我点击“个人资料”图标时发生

这是控制台的整个错误:

这是我的代码:

 GestureDetector(
                        behavior: HitTestBehavior.translucent,
                        onTapDown: (details) {
                          tapOffset = details.globalPosition;
                        },
                        child: IconButton(
                          onPressed: () {
                            showMenu(
                                position: RelativeRect.fromLTRB(
                                  tapOffset!.dx -
                                      150, // assuming popUp width, can be controll by bottom `constraints` providing same width on min and max
                                  64,
                                  tapOffset?.dx ?? 0,
                                  0,
                                ),
                                constraints: const BoxConstraints(
                                  maxWidth: 600,
                                ),
                                context: context,
                                shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(10)),
                                items: [
                                  PopupMenuItem(
                                    child: ListTile(
                                      title: Text(
                                        'Log in',
                                        style: GoogleFonts.nunito(
                                          color: const Color(0xff3B3B3B),
                                          fontSize: 16.0,
                                          fontWeight: FontWeight.w500,
                                        ),
                                      ),
                                      trailing: const Icon(
                                        Icons.login_outlined,
                                        color: Color(0xff3B3B3B),
                                        size: 20,
                                        semanticLabel: 'Pomodoro timer Log in',
                                      ),
                                      contentPadding: EdgeInsets.zero,
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) =>
                                                  const Profile()),
                                        );
                                      },
                                    ),
                                  ),
                                  PopupMenuItem(
                                    child: ListTile(
                                      title: Text(
                                        'Log out',
                                        style: GoogleFonts.nunito(
                                          color: const Color(0xff3B3B3B),
                                          fontSize: 16.0,
                                          fontWeight: FontWeight.w500,
                                        ),
                                      ),
                                      trailing: const Icon(
                                        Icons.logout_outlined,
                                        color: Color(0xff3B3B3B),
                                        size: 20,
                                        semanticLabel: 'Pomodoro timer Log out',
                                      ),
                                      contentPadding: EdgeInsets.zero,
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) =>
                                                  const Text('Log out')),
                                        );
                                      },
                                    ),
                                  ),
                                  PopupMenuItem(
                                    child: ResponsiveWeb(
                                      child: ListTile(
                                        title: Text(
                                          'Go premium',
                                          style: GoogleFonts.nunito(
                                            color: const Color(0xff3B3B3B),
                                            fontSize: 16.0,
                                            fontWeight: FontWeight.w500,
                                          ),
                                        ),
                                        // ignore: prefer_const_constructors
                                        trailing: Icon(
                                          Icons.landscape_outlined,
                                          color: const Color(0xff3B3B3B),
                                          size: 20,
                                          semanticLabel:
                                              'Pomodoro timer premium feature',
                                        ),
                                        contentPadding: EdgeInsets.zero,
                                        onTap: () {
                                          Navigator.push(
                                            context,
                                            MaterialPageRoute(
                                                builder: (context) =>
                                                    const HomeScreenStripe()),
                                          );
                                        },
                                      ),
                                    ),
                                  ),
                                  PopupMenuItem(
                                    child: ListTile(
                                      title: Text(
                                        'Delete account',
                                        style: GoogleFonts.nunito(
                                          color: const Color(0xff3B3B3B),
                                          fontSize: 16.0,
                                          fontWeight: FontWeight.w500,
                                        ),
                                      ),
                                      trailing: const Icon(
                                        Icons.delete_forever_outlined,
                                        color: Color(0xff3B3B3B),
                                        size: 20,
                                        semanticLabel:
                                            'Pomodoro timer Delete account',
                                      ),
                                      contentPadding: EdgeInsets.zero,
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) =>
                                                  const Text('Delete account')),
                                        );
                                      },
                                    ),
                                  ),
                                ]);
                          },
                          icon: Tooltip(
                            message: 'Profile',
                            child: Semantics(
                              label: 'Pomodoro timer More',
                              enabled: true,
                              readOnly: true,
                              child: const Icon(
                                  Icons.account_circle_outlined,
                                color: Color(0xff3B3B3B),
                                size: 24,
                                semanticLabel: 'Pomodoro timer Profile',
                                  
                         
                              ),
                            ),
                          ),
                        ),
                      ),

我如何解决这个问题?

谢谢你能提供的任何帮助


共1个答案

匿名用户

您在ttOffset中使用了空检查()。看起来在那一刻,ttOffset是null。您可以使用运算符而不是,并定义一个默认值(就像您在下面的行中所做的那样)

                                position: RelativeRect.fromLTRB(
                                  tapOffset!.dx -
                                      150, // here is the problem
                                  64,
                                  tapOffset?.dx ?? 0,
                                  0,
                                ),