skip to Main Content

I am trying to pass arguments from a the slider page to the MovieInfo page:

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.

My main file:

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

My MovieInfo screen:

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 ."),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}

This is where I made a column of Carousel sliders:

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',
            )
          ],
        ),
      ),
    )
  ],
),

I checked it a lot but no result! I couldn’t figure where the exception is happening.

2

Answers


  1. From the error message and the code you’ve provided, it seems the issue is with how you’ve set up your route generation in the MaterialApp widget.

    1. Route Generation Overlap:
    You’re using both onGenerateRoute and routes. It’s better to stick with one for clarity. I’d recommend using onGenerateRoute since you’re passing arguments.

    2. Undefined Routes:
    Currently, if none of the conditions in onGenerateRoute match, you’re returning null. Instead, consider returning a default route or an error page.

    3. Argument Type:
    Ensure that when you’re navigating to /MovieInfo, you’re passing the correct Arguments type.

    Here’s a revised 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. Navigation Tip:
    When navigating, ensure you’re using:

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

    Lastly, double-check other parts of your code that involve navigation to ensure they’re set up correctly.

    Login or Signup to reply.
  2. Understood. If you’re obligated to use both routes and onGenerateRoute, that’s perfectly fine. The key is to ensure they work harmoniously.

    1. Handling Routes with No Arguments:
    For routes that don’t require arguments, continue using the routes property as you’ve done.

    2. Handling Routes with Arguments:
    For routes that require arguments, use the onGenerateRoute property. Ensure that you handle the route generation properly, especially for the /MovieInfo route.

    3. Undefined Routes:
    When none of the conditions in onGenerateRoute match, you’re returning null. This can lead to the error you’re seeing. Instead, consider handling this scenario by either redirecting to a default route or displaying an error page.

    Here’s a slightly adjusted 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. Navigation:
    When navigating to routes that require arguments, use:

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

    By structuring it this way, you allow both routes and onGenerateRoute to coexist, with onGenerateRoute handling routes that require arguments and routes handling the rest.

    Hope this clears things up! If you encounter any other issues, feel free to ask.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search