我可以将所有参数推送到我声明的页面。问题是产品的id没有传递到flutter web中的url。当我单击Favorites类中的按钮时,url变为http://localhost:5000/#/prod而不是http://localhost:5000/#/prod/rebEZ0Wswa5UXKfkKjSV(url的最后一部分是productId(
const String HomeRoute = '/';
const String FavoritesRoute = '/favs';
const String ProductDetailRoute = '/prod';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
initialRoute: HomeRoute,
routes: {
HomeRoute: (context) => HomePage(),
FavoritesRoute: (context) => Favorites(),
ProductDetailRoute: (context) =>
ProductDetails(ModalRoute.of(context).settings.arguments),
},
);
}
}
class ProductDetails extends StatefulWidget {
final productDetailsName;
final productDetailsImage;
final productDetailsoldPrice;
var productDetailsPrice;
final productDetailsDesc;
final productDetailsQty;
final productId;
final productTime;
var productDetailsWinner;
ProductDetails(this.productId,
{this.productDetailsName,
this.productDetailsImage,
this.productDetailsoldPrice,
this.productDetailsPrice,
this.productDetailsDesc,
this.productDetailsQty,
this.productTime,
this.productDetailsWinner, arguments});
class Favorites extends StatefulWidget {
...
...
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.pushNamed(context,ProductDetailRoute,
arguments: ProductDetails(
snapshot.data[index]["productId"],
productDetailsName: snapshot.data[index]["name"],
productDetailsImage: snapshot.data[index]["image"],
productDetailsoldPrice: snapshot.data[index]["oldPrice"],
productDetailsPrice: snapshot.data[index]["price"],
productDetailsDesc: snapshot.data[index]["prodDesc"],
),
),
},
),
);
}
}
Navigator.pushNamed方法在上面的代码中接受2个强制参数(BuildContext上下文、String routeName(和一个可选参数(Object?参数(:
Navigator.pushNamed(context,ProductDetailRoute,
arguments: ProductDetails(
snapshot.data[index]["productId"],
productDetailsName: snapshot.data[index]["name"],
productDetailsImage: snapshot.data[index]["image"],
productDetailsoldPrice: snapshot.data[index]["oldPrice"],
productDetailsPrice: snapshot.data[index]["price"],
productDetailsDesc: snapshot.data[index]["prodDesc"],
),
),
},
路由名称将保留此变量ProductDetailRoute尝试使其成为一个获取产品id并返回的函数,如下所示:
String _getProductDetailRouteName(String id){
return '/prod/$id';
}
并将其分配给
Navigator.pushNamed(context,_getProductDetailRouteName(id),
arguments: ProductDetails(
snapshot.data[index]["productId"],
productDetailsName: snapshot.data[index]["name"],
productDetailsImage: snapshot.data[index]["image"],
productDetailsoldPrice: snapshot.data[index]["oldPrice"],
productDetailsPrice: snapshot.data[index]["price"],
productDetailsDesc: snapshot.data[index]["prodDesc"],
),
),
},