In below code I facing problem when I access the map in body argument it will show error object? type can't be assign to widget? type so I use as Widget after running the I get the error which I asking here, so how to access the widget(constructor here) without getting these error.
import 'package:flutter/material.dart';
import '../screens/categories_screen.dart';
import '../screens/favorites_screen.dart';
class TabsScreenBottom extends StatefulWidget {
const TabsScreenBottom({Key? key}) : super(key: key);
@override
State<TabsScreenBottom> createState() => _TabsScreenBottomState();
}
class _TabsScreenBottomState extends State<TabsScreenBottom> {
final List<Map<String, Object>> _pages = const [
{
'page': CategoriesScreen(),
'title': 'Categories',
},
{
'page': FavoritesScreen(),
'title': 'My Favorites',
},
];
int _selectedPageIndex = 0;
void _selectPage(int index) {
setState(() {
_selectedPageIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Meal'),
),
body: _pages[_selectedPageIndex]['page'] as Widget,
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Theme.of(context).colorScheme.primary,
selectedItemColor: Theme.of(context).colorScheme.secondary,
unselectedItemColor: Colors.white,
currentIndex: _selectedPageIndex,
// type: BottomNavigationBarType.shifting,
// giving animation when tab is changed
onTap: _selectPage,
items: [
BottomNavigationBarItem(
backgroundColor: Theme.of(context).colorScheme.primary,
icon: const Icon(
Icons.category,
),
label: 'Categorey',
),
BottomNavigationBarItem(
// if your use type shifting
backgroundColor: Theme.of(context).colorScheme.primary,
icon: const Icon(
Icons.star,
),
label: 'Favorites',
),
],
),
);
}
}
CodePudding user response:
Once change your List<Map<String, Object>> to List<Map<String, dynamic>>
I hope this things are solve your issue.
CodePudding user response:
try to change List<Map<String, Object>>
to List<Map<String, dynamic>>
and it should work.
Coming to your question:
Why it does't work with Object type ?
Lets look at the list item you have
{
'page': CategoriesScreen(),
'title': 'Categories',
},
Here on the left end side you have String
field and hence the same we've used in the Map
Now if you will notice right end data type there are 2 type of values
CategoriesScreen()
this is a type ofWidget
'Categories'
this is type ofString
Hence because we have multiple type of values, we need a certain type
that can adapt both that's why we've used dynamic