【问题标题】:Continuous error of Null check operator used on a null value用于空值的空检查运算符的连续错误
【发布时间】:2021-06-13 16:35:10
【问题描述】:

我正在制作一个带有页面视图和自定义底部导航栏 CustomBottomNavigator 的应用。更改页面时图标会更改颜色,但是每当我为单个 CustomBottomNavigatorItem 实现 Ontap 方法时,它都会提供一个用于空值的空检查运算符。错误附加在代码之后。我不明白即使在观看了几个视频并查看了文档之后也没有任何安全性

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart'as firebase_auth;

import 'package:flutter_fashion/main.dart';


class CustomBottomNavigator extends StatefulWidget {

  final int tab;
  final Function(int) tabPressed;
  const CustomBottomNavigator({Key? key, required this.tab, required this.tabPressed}) : super(key: key);

  @override
  _CustomBottomNavigatorState createState() => _CustomBottomNavigatorState();
}

class _CustomBottomNavigatorState extends State<CustomBottomNavigator> {
  int _selectedTab=0;
  @override
  Widget build(BuildContext context) {
    _selectedTab=widget.tab;
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(12), topRight: Radius.circular(12)),
          boxShadow: [
            BoxShadow(
                color: Colors.black.withOpacity(0.05),
                spreadRadius: 1.0,
                blurRadius: 30.0)
          ]),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          CustomBottomNavigatorItem(
            icon: Icons.home_outlined,
            selected: _selectedTab==0?true:false,
            onPressed: () {
              widget.tabPressed(0);
            },
          ),
          CustomBottomNavigatorItem(
            icon: Icons.code_rounded,
            selected: _selectedTab==1?true:false,
            onPressed: () {
              widget.tabPressed(1);
            },
          ),
          CustomBottomNavigatorItem(
            icon: Icons.bookmark_border_rounded,
            selected: _selectedTab==2?true:false,
            onPressed: () {
              widget.tabPressed(2);
            },
          ),
          CustomBottomNavigatorItem(
            icon: Icons.logout_rounded,
            selected: _selectedTab==3?true:false,
            onPressed: () {
              firebase_auth.FirebaseAuth.instance.signOut();
              Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(
                      builder: (builder) => MyApp()),
                      (route) => false);
            },
          ),
        ],
      ),
    );
  }
}


class CustomBottomNavigatorItem extends StatelessWidget {
  final IconData icon;
  final bool selected;
  final Function onPressed;
  CustomBottomNavigatorItem(
      {required this.icon, required this.selected, required this.onPressed,});
  @override
  Widget build(BuildContext context) {
    bool _selected = selected;
    return GestureDetector(
      onTap: () => onPressed,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 24, vertical: 28),
        decoration: BoxDecoration(
            border: Border(
                top: BorderSide(
                    color: _selected
                        ? Theme.of(context).accentColor
                        : Colors.transparent,
                    width: 2.0))),
        child: Icon(
          icon,
          size: 24,
          color: _selected ? Theme.of(context).accentColor : Colors.black,
        ),
      ),
    );
  }
}

主页

import 'package:flutter/material.dart';
import 'package:flutter_fashion/Views/Widgets/bottom_navigator.dart';
import 'package:flutter_fashion/Views/Widgets/page_view_tabs/ml_page_tab.dart';
import 'package:flutter_fashion/Views/Widgets/page_view_tabs/saved_page_tab.dart';
import 'package:flutter_fashion/Views/Widgets/page_view_tabs/home_page_tab.dart';


class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late final PageController _tabPageController;
  late int _selectedTab=0;

  @override
  void initState() {
    _tabPageController=PageController();
    super.initState();
  }

  @override
  void dispose() {
    _tabPageController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: PageView(
                controller: _tabPageController,
                onPageChanged: (num){
                  setState(() {
                    _selectedTab=num;
                  });
                },
                children: [
                  HomeTab(),
                  MachineLearningTab(),
                  SavedTab(),
                ],
              ),
            ),
            CustomBottomNavigator(tab: _selectedTab,tabPressed: (num){
              _tabPageController.animateToPage(num, duration: Duration(milliseconds: 300), curve: Curves.easeOutCubic);
            },)
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

  • 问题似乎不在于您发布的代码,而在于您的主页代码,可能来自使用某种自定义滚动小部件。自 null-safety 以来,我发现 Flutter 库中出现了类似的错误,并且必须找到使用 Flutter 提供的可滚动小部件来完成工作的方法。有关详细信息,请参阅此问题:github.com/flutter/flutter/issues/66250
  • 看到我在错误信息之后添加了主页我认为它不是由于主页而发生的,如果您注意到应用程序也注销了,那么所有手势检测器都在初始化时被点击

标签: flutter dart flutter-layout flutter-state


【解决方案1】:

对空值使用空检查运算符通常在您对可空标识符使用空检查运算符(!)时发生异常,我怀疑错误原因在这一行:

class CustomBottomNavigatorItem extends StatelessWidget {
...
  @override
  Widget build(BuildContext context) {
...
      onTap: onPressed(),
...
}

应该是:

class CustomBottomNavigatorItem extends StatelessWidget {
...
  @override
  Widget build(BuildContext context) {
...
      onTap: onPressed,
...
}

请注意,我省略了 onPressed 函数上的 '()',因为添加它只是意味着调用它,因此您传递的是 onPressed 函数的结果值,而不是函数本身。

【讨论】:

  • 参数类型'Function'不能赋值给参数类型'void Function()?'。
【解决方案2】:

onPressed() 更改为() =&gt; onPressed

  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => onPressed,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 24, vertical: 28),
        decoration: BoxDecoration(
          border: Border(
            top: BorderSide(width: 2.0),
          ),
        ),
        child: Icon(
          Icons.send,
          size: 24,
        ),
      ),
    );
  }

尝试将管理注销功能的CustomBottomNavigatorItem 更新为如下内容:

CustomBottomNavigatorItem(
            icon: Icons.logout_rounded,
            selected: _selectedTab==3?true:false,
            onPressed: () {
              WidgetsBinding.instance.addPostFrameCallback(
              (_) {
                Navigator.of(context)
                    .pushReplacementNamed("You Route Name");
              },
            ),
            firebase_auth.FirebaseAuth.instance.signOut();
            },
          ),

【讨论】:

  • 虽然没有错误,但当我点击任何图标时没有任何变化。浏览量都没有变化,也无法注销
  • 嘿@Jaime Ortiz 出现空安全错误,但我无法单击以跨页面移动或注销
  • 它说什么了吗?或者它只是不这样做?
  • 什么都没有,但我无法点击更改页面视图和注销
  • 请尝试新答案
猜你喜欢
  • 1970-01-01
  • 2022-06-19
  • 2021-11-04
  • 1970-01-01
  • 2021-01-24
  • 2022-07-06
  • 2021-12-03
  • 2021-09-01
  • 2022-08-06
相关资源
最近更新 更多