我创建了一个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'。
试图从代码中删除所有常量
1条答案
按热度按时间thtygnil1#
你说你试图从代码中删除所有的const,但你仍然得到一个错误,说你使用const?
请确保从以下部分中删除const:
1.在您的
ScreenHome
中:1.在您的
MMBottomNavigation
中: