flutter - 如何在颤动的另一个页面上从地图中获取价值
问题描述
所以我有一个列表视图,我使用 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()));
}
}
我的列表视图和 onclick
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”的小部件的最简单示例:
// BOILERPLATE CODE TO MAKE THE EXAMPLE RUN
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Maps("THE VALUE"),
),
),
);
}
}
// THIS IS THE CLASS YOU NEED TO LOOK AT:
class Maps extends StatefulWidget {
final String position;
const Maps(this.position);
@override
_MapsState createState() => _MapsState ();
}
class _MapsState extends State<Maps> {
Widget build(BuildContext context) {
return Text("You passed: " + widget.position);
}
}
推荐阅读
- sharepoint - sharepoint 2019 无法从第二个域添加用户
- node.js - 如何让发布请求等到 req.body.value 在运行下一个函数之前加载
- rust - 当我们永远无法同时拥有这两者时,我们如何将可变引用强制转换为不可变引用?
- dynamics-crm - 逻辑应用程序 - 如何从 dynamicax API 连接中获取环境信息
- row-level-security - 使用行级策略进行 Supabase 更新
- c - 可以在 C 中打印一串不可打印的字符
- r - 为什么 plot_ly 饼图总是将 R 中的第 5 个值文本从白色变为黑色?
- javascript - 如何为通过 ajax 加载的多个页面配置 Webpack(每页一个 JS 文件)?
- r - 基于R中小于等于条件的Dplyr过滤器
- python - 如何使用子进程在整个 python 程序中保持命令提示符会话?