skip to Main Content

I am working on NamedRoute but after all the connections and doing It right , I am unable to figure out why this error is occurring .

I tried using Navigator.of(Context).push(MaterialPageRoute()) , its working but Its not workking when i am trying to use Navigator.of(context)
.pushNamed(Product_detail.routeName, arguments: id)

main.dart

import 'package:app4/screens/product_detail.dart';
import 'package:flutter/material.dart';
import 'package:app4/screens/product_description.dart';
import 'package:app4/screens/products.dart';
import 'package:app4/models/product.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primaryColor: Colors.purple,
        colorScheme: ColorScheme.highContrastDark(background: Colors.black12),
        fontFamily: 'Lato',
      ),
      home: Hey(),// this is grid view widget just for defining the gird
      routes: {Product_detail.routeName: (context) => Product_detail()},
    );
  }
}


products.dart

import 'package:flutter/material.dart';
import 'package:app4/screens/product_detail.dart';

class Products extends StatelessWidget {
  String imageUrl;
  String title;
  String id;
  Products(this.imageUrl, this.id, this.title);

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(14),
      child: GestureDetector(
        onTap: () {
          Navigator.of(context)
              .pushNamed(Product_detail.routeName, arguments: id);
        },
        child: GridTile(
          child: Container(
            decoration: BoxDecoration(shape: BoxShape.circle, boxShadow: []),
            child: Image(
              fit: BoxFit.fill,
              height: 60,
              image: NetworkImage(imageUrl),
            ),
          ),
          footer: GridTileBar(
            leading: IconButton(
              icon: Icon(Icons.favorite),
              onPressed: () {},
              color: Theme.of(context).colorScheme.background,
            ),
            backgroundColor: Colors.black54,
            title: Text(
              title,
              textAlign: TextAlign.center,
            ),
            trailing: IconButton(
              icon: Icon(Icons.shopping_bag),
              onPressed: () {},
            ),
          ),
        ),
      ),
    );
  }
}


product_detail.dart

import 'package:flutter/material.dart';
import 'package:app4/models/product.dart';

import 'package:flutter/material.dart';

class Product_detail extends StatelessWidget {
  // const Product_detail({super.key});

  // String id;
  // Product_detail(this.id);

  static const routeName = '/productDetail';

  @override
  Widget build(BuildContext context) {
    String? ideal = ModalRoute.of(context)?.settings.arguments.toString();
    return Scaffold(
      appBar: AppBar(title: Text("$ideal")),
    );
  }
}

2

Answers


  1. Try using Material Page Route. Solved mine by using the article above

    https://docs.flutter.dev/cookbook/navigation/passing-data

    Login or Signup to reply.
  2. Yep, I also face with same error and I can’t solve unit now. So I used Navigator.push() instead of pushNamed().
    But there is one way, if want to use pushNamed() by passing argument, using onGenerateRoute is more clear and readable. Here I will show sample.

    1. Build route_generator file to defined route path.
    2. Pass RouteGenerator.route method in onGenerateRoute parameter of Material() widget.
    3. From onTap() method just call Navigator.pushNamed() method and pass arg.
    4. In product_detail.dart, declare id variable and can use that variable.

    route_generator.dart

    const productDetailRoute = '/productDetailRoute';
    
    class RouteGenerator {
      static Route<dynamic> route(RouteSettings settings) {
        final args = settings.arguments;
    
        switch (settings.name) {
          case productDetailRoute:
            return MaterialPageRoute(
              builder: (context) {
                return Product_detail(
                    id: args as String);
              },
            );
    
          default:
            return _errorRoute();
        }
      }
    
      static Route _errorRoute() {
        return MaterialPageRoute(builder: (context) {
          return Scaffold(
            appBar: AppBar(
              title: const Center(child: Text('Error Page')),
            ),
            body: Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: const [
                  Text('Page not found'),
                  Text('404'),
                ],
              ),
            ),
          );
        },);
      }
    }
    

    main.dart

    Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
          onGenerateRoute: RouteGenerator.route,
        );
      }
    

    products.dart

    onTap: () {
                 Navigator.pushNamed(
                 context, productDetailRoute,
                 arguments: id);
              },
    

    product_detail.dart

    class Product_detail extends StatelessWidget {
    
      String id;
      Product_detail(this.id);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text("$id")),
        );
      }
    }
    

    Note: as another advice, using class name with CamelCase is standard way.

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