当我构建一个flutter应用程序时,有三个屏幕,屏幕A,屏幕B,屏幕C。单击按钮时,它将重定向到下一个屏幕。因此它将重定向A到B。但是不重定向B到C。我不知道原因。显示错误。它显示错误是
Could not find a generator for route RouteSettings("/extractArguments1", Instance of 'ScreenArguments') in the _WidgetsAppState.
我的代码显示如下
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
routes: {
'/extractArguments': (context) => ExtractArgumentsScreen(),
'/extractArguments1': (context) => ExtractArgumentsScreen1(),
},
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("A"),
),
body: Center(
child: new RaisedButton(onPressed: (){
Navigator.pushNamed(context, "/extractArguments",arguments: ScreenArguments("title", "message"));
},
child: new Text("Click"),),
),
);
}
}
class ScreenArguments {
final String title;
final String message;
ScreenArguments(this.title, this.message);
}
class ExtractArgumentsScreen extends StatelessWidget{
ExtractArgumentsScreen({
Key key,
@required this.title,
@required this.message,
}): super(key: key);
final String title;
final String message;
@override
Widget build(BuildContext context) {
final ScreenArguments args = ModalRoute.of(context).settings.arguments;
// return new Scaffold(
// appBar: new AppBar(title: new Text('args.title')),
// body: new Center(child: new Text('args.message'),),
// );
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage1(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage1 extends StatefulWidget {
MyHomePage1({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePage1State createState() => _MyHomePage1State();
}
class _MyHomePage1State extends State<MyHomePage1> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("B"),
),
body: Center(
child: new RaisedButton(onPressed: (){
Navigator.pushNamed(context, "/extractArguments1",arguments: ScreenArguments("title", "message"));
},
child: new Text("Click"),),
),
);
}
}
class ExtractArgumentsScreen1 extends StatelessWidget{
ExtractArgumentsScreen1({
Key key,
@required this.title,
@required this.message,
}): super(key: key);
final String title;
final String message;
@override
Widget build(BuildContext context) {
final ScreenArguments args = ModalRoute.of(context).settings.arguments;
// return new Scaffold(
// appBar: new AppBar(title: new Text('args.title')),
// body: new Center(child: new Text('args.message'),),
// );
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage2(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage2 extends StatefulWidget {
MyHomePage2({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePage2State createState() => _MyHomePage2State();
}
class _MyHomePage2State extends State<MyHomePage2> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("C"),
),
body: Center(
child: new RaisedButton(onPressed: (){
// Navigator.pushNamed(context, "/extractArguments1",arguments: ScreenArguments("title", "message"));
},
child: new Text("Click"),),
),
);
}
}
完全错误是
The following assertion was thrown while handling a gesture:
I/flutter (23854): Could not find a generator for route RouteSettings("/extractArguments1", Instance of
I/flutter (23854): 'ScreenArguments') in the _WidgetsAppState.
I/flutter (23854): Generators for routes are searched for in the following order:
I/flutter (23854): 1. For the "/" route, the "home" property, if non-null, is used.
I/flutter (23854): 2. Otherwise, the "routes" table is used, if it has an entry for the route.
I/flutter (23854): 3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not
I/flutter (23854): handled by "home" and "routes".
I/flutter (23854): 4. Finally if all else fails onUnknownRoute is called.
I/flutter (23854): Unfortunately, onUnknownRoute was not set.
请帮助我找到问题。
答案 0 :(得分:2)
问题是当您打电话时:
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>%d{ISO8601}</timestamp>
<message/>
<loggerName/>
<context/>
<mdc/>
<pattern>
<pattern>
{
"appVersion": "${build.version}",
"appName": "${build.artifact}",
"resourceType": "${logback.application.resource.type}",
"resourceID": "${logback.application.resource.id}",
"level": "%level",
"hostname": "${logback.server.host}",
"indexType": "${logback.logstash.index.type}"
}
</pattern>
</layout>
</pattern>
<stackTrace/>
</providers>
</encoder>
您正在命名路由中传递参数,但是您无处接受参数。 您需要在onGenerateRoute()函数中提取参数,并将其传递给小部件。 onGenerateRoute()函数根据给定的RouteSettings创建正确的路由。因此,您可以做的是在材料应用程序内部添加onGenerateRoute()函数,如下所示:
Navigator.pushNamed(context, "/extractArguments1",arguments: ScreenArguments("title", "message"));
此函数将处理参数正确地将它们传递到相应的屏幕屏幕。 有关参考,请参见official documentation。