编写Flutter的推动参数转移时发生了错误,但是在查询大量信息后仍未解决问题

发布于 2025-01-24 05:05:09 字数 2168 浏览 0 评论 0原文

我正在构建一个简单的Flutter应用程序,并且使用命名路由来传递参数遇到困难。 我收到了错误消息,

Receiver: Closure: (dynamic) => Home
Tried calling: new MyApp.<anonymous closure>(Instance of 'StatelessElement', arguments: _LinkedHashMap len:4)
Found: new MyApp.<anonymous closure>(dynamic) => Home

我的主接口代码是

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;
                    }
                }
            }
        );
    }
}

从加载到家庭的自动加载,家庭可以导航设置,库和位置。设置,库和位置都可以浏览详细信息。 我希望通过ongererateroute将参数传递,以便我可以轻松完成以下页面中的相应数据处理。但是我现在无法运行整个项目。 我查找了很多,我无法弄清楚正确的代码。这是我第一次写一个扑朔迷离的项目,我希望得到帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文