如何解决'String=>动态类型不是'Widget'类型的子类型
我正在尝试使用命名路线导航到页面。
主文件中的路由:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'FlutterShare',debugShowCheckedModeBanner: false,theme: ThemeData(
primarySwatch: Colors.deepPurple,accentColor: Colors.teal,),home: Home(),routes: {
'/search': (BuildContext context) => Search(),},);
}
}
推送到页面:
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,title: Text('BookClub'),actions: [
IconButton(
icon: Icon(Icons.add),onPressed: () {
Navigator.of(context).pushNamed('/search');
})
],drawer: AppDrawer(),);
}
}
错误:
type '(String) => dynamic' is not a subtype of type 'Widget'
The relevant error-causing widget was
Search lib\main.dart:21
这是我第一次构建应用程序,但我没有解决方案。 main.dart文件中的路由出错。
搜索代码:
class Search extends StatefulWidget {
@override
_SearchState createState() => _SearchState();
}
class _SearchState extends State<Search> {
TextEditingController searchController = TextEditingController();
Future<QuerySnapshot> searchResultsFuture;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).primaryColor.withOpacity(0.8),appBar: buildSearchField(),body: buildNoContent()
);
}
}
AppBar buildSearchField() {
return AppBar(
backgroundColor: Colors.white,title: TextFormField(
controller: searchController,decoration: InputDecoration(
hintText: "Search for a book",filled: true,prefixIcon: Icon(
Icons.account_box,size: 28.0,suffixIcon: IconButton(
icon: Icon(Icons.clear),onPressed: null,onFieldSubmitted: null,);
}
Container buildNoContent() {
final Orientation orientation = MediaQuery.of(context).orientation;
return Container(
child: Center(
child: ListView(
shrinkWrap: true,children: <Widget>[
SvgPicture.asset(
'assets/images/search.svg',height: orientation == Orientation.portrait ? 300.0 : 200.0,Text(
"Find Users",textAlign: TextAlign.center,style: TextStyle(
color: Colors.white,fontStyle: FontStyle.italic,fontWeight: FontWeight.w600,fontSize: 60.0,],);
}
希望这会有所帮助。过去3个小时,我一直处于这种状态,找不到任何解决方法。
解决方法
main.dart。您必须将起始页添加到initialRoute。您可以删除“ home”参数。
initialRoute: HomeScreen(),routes: {
"/search" (context) => Search()
},
因此,其中有home.dart文件
Navigator.of(context).pushNamed('/search');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。