...
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CategoryPage(
snapshot.data[index]),
));
},
...
使用上面的代码,我尝试将服务 ID 和服务类型传递给类别页面 ... class CategoryPage 扩展 StatefulWidget { // 最终字符串 serviceId; // 最终字符串 service_type; final String categorydata;
CategoryPage(data, {key, this.categorydata}) : super(key: key);
// @override
// CategoryPage(categorydata) {
// this.serviceId = categorydata.serviceId;
// this.service_type = categorydata.serviceId;
// }
_CategoryPageState createState() =>
_CategoryPageState(categorydata.toString());
}
class _CategoryPageState extends State<CategoryPage> {
String id = "";
_CategoryPageState(String categorydata) {
this.id = categorydata.serviceId;
}
...
从上面的代码中,我需要获取服务id和服务类型,并显示带有服务类型参数的Category页面的tile。请帮助我实现这个结果
答案 0 :(得分:1)
您必须使用 categorydata
构造函数中的 CategoryPage
参数传递这样的数据。
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CategoryPage(
categorydata: snapshot.data[index]),
));
},
这里你需要在你的 StatefulWidget
类中定义一个 final 变量并从构造函数初始化它;
CategoryPage({key,required this.categorydata}) : super(key: key);
final Album categorydata;
_CategoryPageState createState() =>
_CategoryPageState();
}
然后您可以使用State
关键字class从widget
类方法访问它,您不需要传递它。
class _CategoryPageState extends State<CategoryPage> {
@override
void initState() {
super.initState();
// access them from here
final categorydata = widget.categorydata;
final String serviceId = categorydata.service_id;
final String service_type = categorydata. service_type;
}
@override
Widget build(BuildContext context) {
// access them from here
final categorydata = widget.categorydata;
final String serviceId = categorydata.service_id;
final String service_type = categorydata. service_type;
}
}
答案 1 :(得分:0)
import 'package:flutter/material.dart';
class CategoryPage extends StatefulWidget {
final String serviceId;
final String service_type;
final categorydata;
CategoryPage(
{Key? key,
required this.serviceId,
required this.service_type,
required this.categorydata})
: super(key: key);
@override
_CategoryPageState createState() => _CategoryPageState();
}
class _CategoryPageState extends State<CategoryPage> {
@override
void initState() {
print(widget.service_type);
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
child: null,
);
}
}
你可以通过 widget.varname 直接访问这些变量
答案 2 :(得分:0)
首先,如果您想在 Category Widget 中获取 2 个参数,您应该从上一页传递 2 个参数。 (而且我强烈推荐使用类。如果你想一起传递的项目是相互关联的,或者可以通过某些条件绑定,那么创建一个新类。你实例化这个类,你可以通过并获取它。它更舒服并且可读性强。但可能会晚一些。习惯它需要时间。)
...
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CategoryPage(
snapshot.date[index].id,
snapshot.data[index].service_type),
// snapshot.data[index] ----- replaced with two lines above),
));
},
...
并且需要定义 serviceId 和 serviceType 变量来获取之前传递的参数。
class CategoryPage extends StatefulWidget {
final String serviceId;
final String serviceType;
CategoryPage(this.serviceId, this.serviceType);
然后你可以使用你的 serviceId 和 serviceType 像:
class _CategoryPageState extends State<CategoryPage> {
@override
Widget build(BuildContext context) {
return Container(
child: Text('ID: ${widget.serviceId}'), // widget. needs to be followed by serviceId
);
}
}
或者您可以定义一个新变量并将“serviceId”放入其中。
class _CategoryPageState extends State<CategoryPage> {
var id = widget.serviceId;
@override
Widget build(BuildContext context) {
return Container(
child: Text('ID: ${id}'), //
);
}
}