I am trying to navigate to a page using named route.
Routes in the main file:
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(),
},
);
}
}
Pushing to the page:
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(),
);
}
}
Error:
type '(String) => dynamic' is not a subtype of type 'Widget'
The relevant error-causing widget was
Search lib\main.dart:21
This is my first time building an app and i am not getting a solution. Error is in the routes in main.dart file.
Search code:
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,
),
),
],
),
),
);
}
Hopefully this helps. I am stuck at this from past 3 hours and could not find anything to solve this.
main.dart. You have to add the start page to initialRoute. You can remove the 'home' parameter.
initialRoute: HomeScreen(),
routes: {
"/search" (context) => Search()
},
So, home.dart file in;
Navigator.of(context).pushNamed('/search');