dart 在Flutter中找不到路由错误的生成器

1tuwyuhd  于 12个月前  发布在  Flutter
关注(0)|答案(3)|浏览(128)

我正在尝试将参数从slider页面传递到MovieInfo页面:

I/flutter ( 6196): Could not find a generator for route RouteSettings("/MovieInfo", Instance of 'Arguments') in the _WidgetsAppState.
I/flutter ( 6196): Make sure your root app widget has provided a way to generate
I/flutter ( 6196): this route.
I/flutter ( 6196): Generators for routes are searched for in the following order:
I/flutter ( 6196):  1. For the "/" route, the "home" property, if non-null, is used.
I/flutter ( 6196):  2. Otherwise, the "routes" table is used, if it has an entry for the route.

字符串
我的主文件:

import 'package:flutter/material.dart';
import 'package:movie/arguments.dart';
import 'package:movie/pages/MovieInfo.dart';
import 'package:movie/pages/Serieses.dart';
import 'package:movie/pages/episodes.dart';
import 'package:movie/pages/livetv.dart';
import 'package:movie/pages/types.dart';
import 'pages/Home.dart';
import 'pages/ChooseLocation.dart';
import 'pages/loading.dart';
import 'pages/Movies.dart';
import 'pages/playvideo.dart';

void main() {
  runApp(MaterialApp(
    initialRoute: '/Movies',
    onGenerateRoute: (settings) {
      if (settings.name == '/videoPlay') {
        final args = settings.arguments as Arguments;
        return MaterialPageRoute(
          builder: (context) =>
              VideoPlayerApp(url: args.url, dataSource: args.dataSource),
        );
      } else if (settings.name == '/episodes') {
        final args = settings.arguments as Arguments;
        return MaterialPageRoute(
          builder: (context) =>
              episodes(SeriesName: args.name, SeriesPath: args.path),
        );
      } else if (settings.name == '/MovieInfo') {
        final args = settings.arguments as Arguments;
        return MaterialPageRoute(
            builder: (context) => MovieInfo(name: args.name, path: args.path));
      }
      return null;
    },
    routes: {
      '/': (context) => Loading(),
      '/location': (context) => ChoosLocation(),
      '/home': (context) => Home(),
      '/Movies': (context) => Movies(),
      '/Series': (context) => Series(),
      '/liveTv': (context) => liveTv(),
      '/Types': (context) => Types()
    },
  ));
}


我的MovieInfo屏幕:

import 'package:flutter/material.dart';
import 'dart:ui';

class MovieInfo extends StatelessWidget {
  late String name;
  late String path;
  MovieInfo({required this.name, required this.path});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            expandedHeight: 300,
            flexibleSpace: FlexibleSpaceBar(
              background: BackdropFilter(
                  filter: ImageFilter.blur(sigmaX: 6.0, sigmaY: 6.0),
                  child: ImageFiltered(
                    imageFilter: ImageFilter.blur(sigmaX: 6.0, sigmaY: 6.0),
                    child: Image(
                      image: NetworkImage(path),
                      width: double.maxFinite,
                      fit: BoxFit.cover,
                    ),
                  )),
            ),
          ),
          SliverToBoxAdapter(
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(right: 760),
                  child: ElevatedButton(
                    onPressed: () {},
                    child: Text('watch'),
                  ),
                ),
                Container(
                  child: Text(
                      "In the application main() method, call SystemChrome. setPreferredOrientations() with the list of preferred orientations that your app supports. To lock the device to a single orientation, you can pass a list with a single item. For a list of all the possible values, check out DeviceOrientation ."),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}


这是我做了一列Carnival滑块的地方:

child: Row(
  children: [
    Container(
      width: 200,
      height: double.maxFinite,
      color: Color.fromRGBO(0, 0, 0, 0.5),
      child: Column(children: [
        SizedBox(height: 10),
        ListTile(
          onTap: () {
            _scrollToPosition(200);
          },
          title: Text(
            'Drama',
            style: TextStyle(color: Colors.white),
          ),
        )
      ]),
    ),
    Expanded(
      child: SingleChildScrollView(
        controller: _scrollcontroller,
        child: Column(
          children: [
            SizedBox(
              height: 100,
            ),
            CSlider(
              list: movies,
              name: 'Top movies',
            ),
            CSlider(
              list: movies,
              name: 'Horror',
            ),
            CSlider(
              list: movies,
              name: 'Drama',
            ),
            CSlider(
              list: movies,
              name: 'Kids',
            ),
            CSlider(
              list: movies,
              name: 'Shows',
            )
          ],
        ),
      ),
    )
  ],
),


我检查了很多次,但没有结果!我不知道哪里发生了异常。

jhkqcmku

jhkqcmku1#

从错误消息和您提供的代码来看,问题似乎在于您如何在MaterialApp小部件中设置路由生成。

1.路由生成重叠:

你同时使用了onGenerateRouteroutes。为了清晰起见,最好还是使用一个。我建议使用onGenerateRoute,因为你要传递参数。

2.未定义路由:

目前,如果onGenerateRoute中的条件都不匹配,则返回null。相反,请考虑返回默认路由或错误页面。

3.参数类型:

确保在导航到/MovieInfo时,传递的是正确的Arguments类型。
以下是修改后的onGenerateRoute

onGenerateRoute: (settings) {
  if (settings.arguments is Arguments) {
    final args = settings.arguments as Arguments;
    switch (settings.name) {
      case '/videoPlay':
        return MaterialPageRoute(
          builder: (context) => VideoPlayerApp(url: args.url, dataSource: args.dataSource),
        );
      case '/episodes':
        return MaterialPageRoute(
          builder: (context) => episodes(SeriesName: args.name, SeriesPath: args.path),
        );
      case '/MovieInfo':
        return MaterialPageRoute(
          builder: (context) => MovieInfo(name: args.name, path: args.path),
        );
      default:
        return MaterialPageRoute(builder: (context) => ErrorPage()); // Consider adding an ErrorPage for unmatched routes
    }
  } else {
    return MaterialPageRoute(builder: (context) => ErrorPage()); // Handle cases where arguments aren't of type 'Arguments'
  }
},

字符串

4.导航提示:

导航时,请确保您正在使用:

Navigator.pushNamed(context, '/MovieInfo', arguments: Arguments(name: 'MovieName', path: 'MoviePath'));


最后,仔细检查代码中涉及导航的其他部分,以确保它们设置正确。

bpsygsoo

bpsygsoo2#

如果你必须同时使用routesonGenerateRoute,那也没关系,关键是要确保它们能和谐地工作。

1.无参数路由处理:

对于不需要参数的路由,继续使用routes属性。

2.处理带参数的路由:

对于需要参数的路由,请使用onGenerateRoute属性。确保正确处理路由生成,特别是对于/MovieInfo路由。

3.未定义路由:

onGenerateRoute中的条件都不匹配时,您将返回null。这可能会导致您看到的错误。相反,请考虑通过重定向到默认路由或显示错误页面来处理此情况。
下面是一个稍微调整的onGenerateRoute

onGenerateRoute: (settings) {
  if (settings.arguments is Arguments) {
    final args = settings.arguments as Arguments;
    switch (settings.name) {
      case '/videoPlay':
        return MaterialPageRoute(
          builder: (context) => VideoPlayerApp(url: args.url, dataSource: args.dataSource),
        );
      case '/episodes':
        return MaterialPageRoute(
          builder: (context) => episodes(SeriesName: args.name, SeriesPath: args.path),
        );
      case '/MovieInfo':
        return MaterialPageRoute(
          builder: (context) => MovieInfo(name: args.name, path: args.path),
        );
      default:
        break; // Let the 'routes' property handle it
    }
  }
  return null; // This will allow the 'routes' property to take over for routes without arguments
},

字符串

4.导航:

导航到需要参数的路由时,请使用:用途:

Navigator.pushNamed(context, '/MovieInfo', arguments: Arguments(name: 'MovieName', path: 'MoviePath'));


通过这种结构,您可以允许routesonGenerateRoute共存,onGenerateRoute处理需要参数的路由,routes处理其余路由。

yws3nbqq

yws3nbqq3#

更新:
导致错误的是OrientationBuilder,所以我通过删除OrientationBuilder解决了这个问题。

相关问题