'An error occurred when writing Flutter's PushNamed parameter transfer, but the problem was still not solved after inquiring a lot of information
I'm building a simple flutter app and I'm having trouble passing parameters using named routes. I got the error message
Receiver: Closure: (dynamic) => Home
Tried calling: new MyApp.<anonymous closure>(Instance of 'StatelessElement', arguments: _LinkedHashMap len:4)
Found: new MyApp.<anonymous closure>(dynamic) => Home
My main interface code is
import 'package:world_time/pages/Library.dart';
import 'package:world_time/pages/home.dart';
import 'package:world_time/pages/choose_location.dart';
import 'package:world_time/pages/loading.dart';
import 'package:world_time/pages/details.dart';
import 'package:world_time/pages/setting.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget{
MyApp({Key? key}) : super(key: key);
final routes = {
'/': (context) => Loading(),
'/home': (context) => Home(),
'/setting': (context) => ChangeSetting(),
'/library': (context) => ChooseLibrary(),
'/location': (context) => ChooseLocation(),
'/details': (context,{arguments}) => Details(arguments:arguments),
};
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Loading(),
onGenerateRoute: (RouteSettings settings) {
final String? name = settings.name;
final Function pageContentBuilder = this.routes[name] as Function;
if (pageContentBuilder != null) {
if (settings.arguments != null) {
final Route route = MaterialPageRoute(
builder: (context) => pageContentBuilder(context, arguments: settings.arguments));
return route;
} else {
final Route route = MaterialPageRoute(
builder: (context) => pageContentBuilder(context));
return route;
}
}
}
);
}
}
Automatically load from loading to home, home can navigate setting, library and location. Setting, library and location can all navigate details. I hope to pass parameters through onGenerateRoute, so that I can easily complete the corresponding data processing in the following pages. But I can't run the whole project now. I've looked up a lot and I can't figure out the correct code. This is my first time writing a flutter project and I hope to get help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
