gpt4 book ai didi

flutter - 如果语句在我的 Dart Flutter 代码中被注册为变量?我该如何改变?

转载 作者:行者123 更新时间:2023-12-03 04:21:30 25 4
gpt4 key购买 nike

The highlighted text is giving me errors like those shown in my Problems console. I've never receieved these kinds of issues when dealing with if statements, and I'm wondering why they are no registering as if statements. In one of the errors it says "if is already defined", but its not a variable. How do I solve this? Does it have anything to do with the async functions? I struggled with these.

图像中突出显示的文本给了我类似问题控制台中显示的错误。在处理 if 语句时,我从未收到过这类问题,我想知道为什么它们没有注册为 if 语句。在其中一个错误中,它说“如果已定义”,但它不是变量。我该如何解决这个问题?它与异步功能有什么关系吗?我与这些斗争

我正在尝试为我计划在 flutter 应用程序中实现的 map 请求用户位置数据,但它不起作用:/我无法解决的 ifs 有问题。

Future<bool> assignService(Location loc) async {
bool servicestatus = await loc.serviceEnabled();
return servicestatus;
}

Future<PermissionStatus> assignPermission(Location loc) async {
return await loc.hasPermission();
}

Future<LocationData> assignLocation(Location loc) async {
return await loc.getLocation();
}

Location location = new Location();

var _serviceEnabled = assignService(location);

if (_serviceEnabled != true) {
_serviceEnabled = assignService(location);
if (!_serviceEnabled) {
return;
}
}

var _permissionGranted = assignPermission(location);

if (_permissionGranted == PermissionStatus.denied) async{
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted) {
return;
}
}

var _locationData = assignLocation(location);

更新(上面的代码):
  Future<bool> assignService(Location loc) async {
bool servicestatus = await loc.serviceEnabled();
return servicestatus;
}

Future<PermissionStatus> assignPermission(Location loc) async {
return await loc.hasPermission();
}

Future<LocationData> assignLocation(Location loc) async {
return await loc.getLocation();
}

Location location = Location();

var _serviceEnabled = assignService(location);

var _permissionGranted = assignPermission(location);

最佳答案

您在函数之外编写代码。
只有变量声明可以在函数之外,而不是代码。

例如,您可以这样做:

  void StartService() {
if (_serviceEnabled != true) {
_serviceEnabled = assignService(location);
if (!_serviceEnabled) {
return;
}

if (_permissionGranted == PermissionStatus.denied) async{
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted) {
return;
}
}
}

if 不应该在代码块之外。
你必须把它放在一个函数中。这就是解释你的错误。

如果您需要更多详细信息,请告诉我。

编辑:仅供引用, flutter 中不再需要"new"关键字。

关于flutter - 如果语句在我的 Dart Flutter 代码中被注册为变量?我该如何改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62346049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com