【问题标题】:How to change onPressed to one time function如何将 onPressed 更改为一次性功能
【发布时间】:2021-12-24 20:17:39
【问题描述】:

我正在从 codecanyon 定制 Flutter 电子商务移动应用。 如何将 onPressed 命令更改为一次自动工作的一次功能

onPressed: () async {
          Position position = await Geolocator.getCurrentPosition(
              desiredAccuracy: LocationAccuracy.high);

          await Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => Map(
                        latitude: latitude == null
                            ? position.latitude
                            : double.parse(latitude),
                        longitude: longitude == null
                            ? position.longitude
                            : double.parse(longitude),
                        from: getTranslated(context, 'ADDADDRESS'),
                      )));
          if (mounted) setState(() {});
          List<Placemark> placemark = await placemarkFromCoordinates(
              double.parse(latitude), double.parse(longitude));

          state = placemark[0].administrativeArea;
          country = placemark[0].country;
          pincode = placemark[0].postalCode;
          //  address = placemark[0].name;
          if (mounted)
            setState(() {
              countryC.text = country;
              stateC.text = state;
              pincodeC.text = pincode;
              // addressC.text = address;
            });
        },

【问题讨论】:

  • “将自动工作一次”是什么意思?请你添加完整的代码吗?
  • 你是想说它只能点击一次吗?
  • 代码通过单击图标选择位置打开 google mab。而不是点击图标,我希望 mab 自动打开一次
  • 只需将其设为函数并在 initState 中调用即可。

标签: flutter dart


【解决方案1】:
@override
void initState(){
  super.initState();
_openMap();
}
......
void _openMap() async {
          Position position = await Geolocator.getCurrentPosition(
              desiredAccuracy: LocationAccuracy.high);

          await Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => Map(
                        latitude: latitude == null
                            ? position.latitude
                            : double.parse(latitude),
                        longitude: longitude == null
                            ? position.longitude
                            : double.parse(longitude),
                        from: getTranslated(context, 'ADDADDRESS'),
                      )));
          if (mounted) setState(() {});
          List<Placemark> placemark = await placemarkFromCoordinates(
              double.parse(latitude), double.parse(longitude));

          state = placemark[0].administrativeArea;
          country = placemark[0].country;
          pincode = placemark[0].postalCode;
          //  address = placemark[0].name;
          if (mounted)
            setState(() {
              countryC.text = country;
              stateC.text = state;
              pincodeC.text = pincode;
              // addressC.text = address;
            });
        }

.......
.....

onPressed:_openMap

【讨论】:

  • 如果有不明白的问题,请在cmets中要求澄清。
  • 我试图这样做,但我遇到了这个错误错误:将位置参数放在命名参数之前。尝试将位置参数移到命名参数之前,或为参数添加名称。 void _openMap() 异步 {
猜你喜欢
  • 2021-08-12
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 2019-12-13
  • 2019-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多