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
Try using Material Page Route. Solved mine by using the article above
https://docs.flutter.dev/cookbook/navigation/passing-data
Yep, I also face with same error and I can’t solve unit now. So I used
Navigator.push()
instead ofpushNamed()
.But there is one way, if want to use
pushNamed()
by passing argument, usingonGenerateRoute
is more clear and readable. Here I will show sample.RouteGenerator.route
method inonGenerateRoute
parameter ofMaterial()
widget.onTap()
method just callNavigator.pushNamed()
method and passarg
.route_generator.dart
main.dart
products.dart
product_detail.dart
Note: as another advice, using class name with CamelCase is standard way.