对空值使用的空检查运算符的连续错误

时间:2021-06-13 16:35:10

标签: flutter dart flutter-layout flutter-state

我正在制作一个带有页面视图和自定义底部导航栏的应用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);
            },)
          ],
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分: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 函数的结果值,而不是函数本身。

答案 1 :(得分:0)

onPressed() 改为 () => 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();
            },
          ),