【发布时间】:2021-01-01 21:38:18
【问题描述】:
所以我有一个列表视图,我使用 Future 来获取数据,它显示得很好。现在我正在尝试将单击项目上的值从列表视图页面解析到另一个页面,该页面将显示项目单击的详细信息。请问我该如何实现?
未来
List dealData = List();
Future<String> _fetchComment() async {
setState(() {
isLoading = true;
debugPrint("emirate state");
});
try {
debugPrint("emirate try");
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
print('connected');
debugPrint("emirate connect");
String url;
debugPrint("my select:$_mySelection");
if (_mySelection == null && _myFeatureSelection == null) {
url = "my rest api";
} else if (_myFeatureSelection != null) {
url =
"my rest api";
_mySelection = null;
} else if (_mySelection != null && _myFeatureSelection == null) {
url = "my rest api";
}
print("our url:$url");
var res = await http
.get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
var resBody = json.decode(res.body);
debugPrint("emirate url:$url");
setState(() {
dealData = resBody;
isLoading = false;
});
print(resBody);
debugPrint("emirate:$resBody");
return "Sucess";
} else {
throw Exception('Failed to load profile');
}
} on SocketException catch (_) {
print('not connected');
setState(() => isLoading = false);
Navigator.popUntil(
context, (_) => !Navigator.canPop(context));
Navigator.pushReplacement(
context,
new MaterialPageRoute(
builder: (BuildContext context) => NoInternet()));
}
}
我的列表视图和点击
dealData
.map(
(position) => FutureBuilder<String>(
future: getDistance(
position["lat"],
position["lng"])
.then((value) =>
value.toString()),
builder: (context, snapshot) {
double myrate = double.parse(
position["ratings"] ==
null
? "0"
: position["ratings"]);
return Container(
child:Card(child:
GestureDetector(
onTap: () {
print(position); // position printed here
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext ctx) => Maps(position)));
},
).toList(),
我的地图课
class mapsFinal extends StatefulWidget {
final int position;
const mapsFinal(this.position);
@override
_MapsState createState() => _MapsState ();
}
class _MapsState extends State<mapsFinal> {
Widget build(BuildContext context) {
return Text("title" + widget.position.toString());
}
}
我需要第二个页面来显示我在此处单击的项目。
【问题讨论】:
-
你能澄清你的问题吗?我看到很多关于接收数据的代码,但对你的实际问题却很少。您似乎有可以执行您想要的代码,它有什么问题?
-
我的代码运行良好,我只是在寻找如何在“地图”页面上接收“位置”。
-
你已经这样做了,不是吗?
Maps(position)不就是这样做的吗? -
是的,我用 Navigator.push(context, MaterialPageRoute(builder: (BuildContext ctx) => Maps(position)));已经,但我不知道如何在另一个页面“地图”中接收值
-
“接收”是什么意思?你的代码编译了吗?然后你收到了。不是吗?然后也许发布您的地图课程。