英文:
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 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
// ignore: must_be_immutable
class CustomNavbar extends StatefulWidget {
  int selectedIndex;
  Function 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: ''),
            ],
          ),
        ),
      ),
    );
  }
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论