I facing this error
Error: The argument type 'Object?' can't be assigned to the parameter type 'Widget?'.
tabs_screen.dart:34
Object
is fromdart:core
.Widget
is frompackage:flutter/src/widgets/framework.dart
('/C:/flutter_windows_3.3.4-stable/flutter/packages/flutter/lib/src/widgets/framework.dart').
framework.dart:1
body: _pages[_selectPageIndex]['page'].
Code
class TapScreen extends StatefulWidget {
const TapScreen({super.key});
@override
State<TapScreen> createState() => _TapScreenState();
}
List<Map<String, Object>> _pages = [
{'page': CategoriesScreen(), 'title': 'Catergories'},
{'page': FavoitesScreen(), 'title': 'your Favorites'}
];
class _TapScreenState extends State<TapScreen> {
int _selectPageIndex = 0;
void _selectPage(int index) {
setState(() {
_selectPageIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("meal"),
),
body: _pages[_selectPageIndex]['page'],
bottomNavigationBar: BottomNavigationBar(
onTap: _selectPage,
backgroundColor: Theme.of(context).primaryColor,
unselectedItemColor: Colors.white,
selectedItemColor: Colors.amber,
currentIndex: _selectPageIndex,
type: BottomNavigationBarType.shifting,
items: [
BottomNavigationBarItem(
backgroundColor: Theme.of(context).primaryColor,
icon: Icon(Icons.star),
label: 'Favorites',
),
BottomNavigationBarItem(
backgroundColor: Theme.of(context).primaryColor,
icon: Icon(Icons.category),
label: 'Favorites',
),
],
),
);
}
}
2
Answers
You can provide default widget on null case.
In Dart
Object
can be anything and the body of the scaffold expects only the typeWidget
thus causing the error. It is always better to useWidget
type annotation.In your case, Check if that
Object
is a widget type or notFull code