flutter 不能将类型为“Null”的值赋给常量构造函数中类型为“ValueListsustainable”的参数< int>

cyvaqqii  于 2023-05-18  发布在  Flutter
关注(0)|答案(1)|浏览(256)

我创建了一个Valuenotifier,它显示此错误

class ScreenHome extends StatelessWidget {
  const ScreenHome({super.key});

  static ValueNotifier<int> selectedIndexNotifier = ValueNotifier(0);

  // ignore: unused_field
  final _pages = const [
    ScreenTransation(),
    ScreenCatagaries(),
  ];

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
        bottomNavigationBar: MMBottomNavigation(),
        body: SafeArea(
          child: ValueListenableBuilder(
            valueListenable: selectedIndexNotifier,
            builder: (BuildContext context, int updatedIndex, _) {
              return _pages[updatedIndex];
            },
          ),
        ));
  }
}
class MMBottomNavigation extends StatelessWidget {
  const MMBottomNavigation({super.key});

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
      valueListenable: ScreenHome.selectedIndexNotifier,
      builder: (BuildContext ctx, int updatedIndex, Widget? _) {
        return BottomNavigationBar(
            selectedItemColor: Colors.purple,
            unselectedItemColor: Colors.deepPurple[100],
            currentIndex: updatedIndex,
            onTap: (newIndex) {
              ScreenHome.selectedIndexNotifier.value = newIndex;
            },
            items: const[
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                label: 'Transation',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.category),
                label: 'Categories',
              ),
            ]);
      },
    );
  }
}

不能将“Null”类型的值赋给常量构造函数中“ValueListable”类型的参数。尝试使用子类型,或删除关键字'const'。
试图从代码中删除所有常量

thtygnil

thtygnil1#

你说你试图从代码中删除所有的const,但你仍然得到一个错误,说你使用const?
请确保从以下部分中删除const:
1.在您的ScreenHome中:

final _pages = const [
 ScreenTransation(),
 ScreenCatagaries(),
];  //remove const

return const Scaffold() //remove const

1.在您的MMBottomNavigation中:

items: const [ 
      BottomNavigationBarItem(), 
      BottomNavigationBarItem(), ] //remove const

相关问题