Home > Blockchain >  Passing Arguments between pages but show this error
Passing Arguments between pages but show this error

Time:10-29

I m trying to pass arguments between pages but I m getting this error:

FlutterError (Could not find a generator for route RouteSettings("detail", Instance of 'Commodity') in the _WidgetsAppState. Make sure your root app widget has provided a way to generate this route. Generators for routes are searched for in the following order:

  1. For the "/" route, the "home" property, if non-null, is used.
  2. Otherwise, the "routes" table is used, if it has an entry for the route.
  3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".
  4. Finally if all else fails onUnknownRoute is called. Unfortunately, onUnknownRoute was not set.)

I don't know why because 'detail' page already setted

This is my Main App

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      initialRoute: WelcomePage.routeName,
      routes: {
        '/welcome': (_) => const WelcomePage(),
        'detail': (_) => SubCommoditiePage(),
      },
    );
  }
}

This is my From Page

class DisplayOptions extends StatelessWidget {
  final List<Commodity> _optionsToDisplay;
  const DisplayOptions(this._optionsToDisplay);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: _optionsToDisplay.length,
        itemBuilder: (context, index) {
          final opt = _optionsToDisplay[index];

          return ListTile(
              title: Text(opt.label),
              subtitle: Text(opt.shortLabel),
              onTap: () {
                Navigator.pushNamed(context, 'detail', arguments: _optionsToDisplay[index]);
              });
        });
  }
}

This is my Destination Page


class SubCommoditiePage extends StatefulWidget {
  const SubCommoditiePage({Key? key}) : super(key: key);

  static const String routenName = 'detail';
  static Route route() {
    return MaterialPageRoute(
        builder: (_) => const SubCommoditiePage(),
        settings: const RouteSettings(name: routenName));
  }

  @override
  _SubCommoditiePageState createState() => _SubCommoditiePageState();
}

class _SubCommoditiePageState extends State<SubCommoditiePage> {
  @override
  Widget build(BuildContext context) {
    final Commodity commoditySeleted =
        ModalRoute.of(context)?.settings.arguments as Commodity;

    

    return Scaffold(
      appBar: AppBar(
        title: Text(commoditySeleted.label),
      ),
    );
  }
}

CodePudding user response:

The way I was able to solve it was:

I evidently didn't know why flutter didn't recognize my 'detail' page.

And I had to do it this way:

This is my From Page

in my ontap, I used

Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => SubCommoditiePage(
                              seleted: _optionsToDisplay[index],
                            )));

like that:

class DisplayOptions extends StatelessWidget {
  final List<Commodity> _optionsToDisplay;
  const DisplayOptions(this._optionsToDisplay);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: _optionsToDisplay.length,
        itemBuilder: (context, index) {
          final opt = _optionsToDisplay[index];

          return ListTile(
              title: Text(opt.label),
              subtitle: Text(opt.shortLabel),
              onTap: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => SubCommoditiePage(
                              seleted: _optionsToDisplay[index],
                            )));
              });
        });
  }
}

adding to my page: final Commodity seleted; and with widget.selected... I was able to use it perfectly!

class SubCommoditiePage extends StatefulWidget {
  final Commodity seleted;
  const SubCommoditiePage({required this.seleted, Key? key}) : super(key: key);

  static const String routenName = 'detail';

  @override
  _SubCommoditiePageState createState() => _SubCommoditiePageState();
}

class _SubCommoditiePageState extends State<SubCommoditiePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.seleted.label),
          backgroundColor: const Color.fromRGBO(37, 59, 128, 5),
        ),
        body: ListView.builder(
            itemCount: widget.seleted.subCommodities.length,
            itemBuilder: (context, index) {
              final opt = widget.seleted.subCommodities[index];
              return ListTile(
                title: Text(opt.label),
                onTap: () {},
              );
            }));
  }
}

CodePudding user response:

For initial/homepage, use /. In this case your WelcomePage.routeName will be /;

and route

 routes: {
        WelcomePage.routeName: (_) => WelcomePage(),
         ..........
      },

You don't need to pass initialRoute while the root '/' will be selected.

For more about named-routes

  • Related