“The argument type ‘Function’ can’t be assigned to the parameter type ‘void Function(int)?'”

huangapple go评论68阅读模式
英文:

Got this error "The argument type 'Function' can't be assigned to the parameter type 'void Function(int)?'"

问题

错误在这一行:onTap: widget.onItemTapped,,我认为与这一行有关:Function onItemTapped;

要解决这个错误,你可以将Function更改为void Function(int),如下所示:

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

class CustomNavbar extends StatefulWidget {
  int selectedIndex;
  void Function(int) onItemTapped; // 更改此行的类型

  CustomNavbar({super.key, required this.selectedIndex, required this.onItemTapped});

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

class _CustomNavbarState extends State<CustomNavbar> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(left: 60, right: 60, bottom: 20),
      color: Colors.transparent,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(20),
        child: SizedBox(
          height: 70,
          child: BottomNavigationBar(
            currentIndex: widget.selectedIndex,
            onTap: widget.onItemTapped,
            showSelectedLabels: false,
            showUnselectedLabels: false,
            elevation: 0,
            items: [
              (widget.selectedIndex == 0)
                  ? BottomNavigationBarItem(icon: SvgPicture.asset('assets/icons/home-filled.svg'), label: '')
                  : BottomNavigationBarItem(icon: SvgPicture.asset('assets/icons/home.svg'), label: ''),
              (widget.selectedIndex == 1)
                  ? BottomNavigationBarItem(icon: SvgPicture.asset('assets/icons/discover-filled.svg'), label: '')
                  : BottomNavigationBarItem(icon: SvgPicture.asset('assets/icons/discover.svg'), label: ''),
              (widget.selectedIndex == 2)
                  ? BottomNavigationBarItem(icon: SvgPicture.asset('assets/icons/bookmark-filled.svg'), label: '')
                  : BottomNavigationBarItem(icon: SvgPicture.asset('assets/icons/bookmark.svg'), label: ''),
            ],
          ),
        ),
      ),
    );
  }
}

通过将Function更改为void Function(int),你将函数参数的类型与onTap所需的类型匹配,从而解决了这个错误。

英文:

I'm new to Flutter and I'm trying to create a custom bottom navbar and I'm getting this error "The argument type 'Function' can't be assigned to the parameter type 'void Function(int)?'".

import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter_svg/flutter_svg.dart&#39;;

// ignore: must_be_immutable
class CustomNavbar extends StatefulWidget {
  int selectedIndex;
  Function onItemTapped;
  CustomNavbar({super.key, required this.selectedIndex, required this.onItemTapped});

  @override
  _CustomNavbarState createState() =&gt; _CustomNavbarState();
}

class _CustomNavbarState extends State&lt;CustomNavbar&gt; {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(left: 60, right: 60, bottom: 20),
      color: Colors.transparent,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(20),
        child: SizedBox(
          height: 70,
          child: BottomNavigationBar(
            currentIndex: widget.selectedIndex,
            onTap: widget.onItemTapped,
            showSelectedLabels: false,
            showUnselectedLabels: false,
            elevation: 0,
            items: [
              (widget.selectedIndex == 0)
                  ? BottomNavigationBarItem(icon: SvgPicture.asset(&#39;assets/icons/home-filled.svg&#39;), label: &#39;&#39;)
                  : BottomNavigationBarItem(icon: SvgPicture.asset(&#39;assets/icons/home.svg&#39;), label: &#39;&#39;),
              (widget.selectedIndex == 1)
                  ? BottomNavigationBarItem(icon: SvgPicture.asset(&#39;assets/icons/discover-filled.svg&#39;), label: &#39;&#39;)
                  : BottomNavigationBarItem(icon: SvgPicture.asset(&#39;assets/icons/discover.svg&#39;), label: &#39;&#39;),
              (widget.selectedIndex == 2)
                  ? BottomNavigationBarItem(icon: SvgPicture.asset(&#39;assets/icons/bookmark-filled.svg&#39;), label: &#39;&#39;)
                  : BottomNavigationBarItem(icon: SvgPicture.asset(&#39;assets/icons/bookmark.svg&#39;), label: &#39;&#39;),
            ],
          ),
        ),
      ),
    );
  }
}

The error is in the line onTap: widget.onItemTapped, and I think it has something to do with this line Function onItemTapped;

So how to fix this error? Any help would be appreciated.
Thanks

答案1

得分: 1

onTap 期望的是 Function(int),所以您可以替换:

> Function onItemTapped;

> void Function(int) onItemTapped;

或者您可以替换:

> onTap: widget.onItemTapped;

> onTap:(int value) {
widget.onItemTapped();
}

如果您的函数不需要使用 int 值。

英文:

onTap expects Function(int), so you can replace

> Function onItemTapped;

with

> void Function(int) onItemTapped;

Or you can replace

> onTap: widget.onItemTapped,

with

> onTap:(int value) {
widget.onItemTapped();
}

if you don't need to use int value in your function

huangapple
  • 本文由 发表于 2023年6月8日 13:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定