英文:
I making Restaurant app form Udemy tutorial it show me this error
问题
我面临这个错误
错误: 无法将参数类型'Object?'分配给参数类型'Widget?'。
tabs_screen.dart:34
Object
来自dart:core
。Widget
来自package: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']。
代码
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',
),
],
),
);
}
}
英文:
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',
),
],
),
);
}
}
答案1
得分: 2
在Dart中,Object
可以是任何东西,而scaffold
的主体仅期望Widget
类型,因此会导致错误。最好始终使用Widget
类型注释。
在您的情况下,检查该Object
是否为widget
类型或不是
_pages[_selectPageIndex]['page'] is Widget ? _pages[_selectPageIndex]['page'] as Widget : Text("Page not found");
完整代码
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'] is Widget ? _pages[_selectPageIndex]['page'] as Widget : Text("Page not found"), // 在这里更改
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',
),
],
),
);
}
}
英文:
In Dart Object
can be anything and the body of the scaffold expects only the type Widget
thus causing the error. It is always better to use Widget
type annotation.
In your case, Check if that Object
is a widget type or not
_pages[_selectPageIndex]['page'] is Widget ? _pages[_selectPageIndex]['page'] as Widget : Text("Page not found");
Full 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'] is Widget ? _pages[_selectPageIndex]['page'] as Widget : Text("Page not found"), // Change here
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
得分: 1
body: _pages[_selectPageIndex]['page'] as Widget? ?? Text("not found"),
英文:
You can provide default widget on null case.
body: _pages[_selectPageIndex]['page'] as Widget? ?? Text("not found"),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论