小编Pur*_*tto的帖子

获取异步函数的 NULL 值(使用 await 后)然后更新为新值

当我运行我的应用程序时,它会抛出很多错误,我的设备上会出现红色/黄色错误屏幕,它会自动刷新并向我显示预期的输出。

从日志中我可以看到,首先我的对象返回为空,稍后以某种方式更新并获得输出。

我最近开始了 Android Dev (Flutter)

我尝试遵循一些在线指南并阅读有关异步响应的相关问题,但没有任何故障排除对我有帮助。我最大的问题是我无法弄清楚到底是什么问题。

在我的 _AppState 类中:

  void initState() {
    super.initState();
    fetchData();
  }

  fetchData() async {
    var cityUrl = "http://ip-api.com/json/";
    var cityRes = await http.get(cityUrl);
    var cityDecodedJson = jsonDecode(cityRes.body);
    weatherCity = WeatherCity.fromJson(cityDecodedJson);
    print(weatherCity.city);
    var weatherUrl = "https://api.openweathermap.org/data/2.5/weather?q=" +
        weatherCity.city +
        "," +
        weatherCity.countryCode +
        "&appid=" +
        //Calling open weather map's API key from apikey.dart
        weatherKey;
    var res = await http.get(weatherUrl);
    var decodedJson = jsonDecode(res.body);
    weatherData = WeatherData.fromJson(decodedJson);
    print(weatherData.weather[0].main);
    setState(() {});
  }
Run Code Online (Sandbox Code Playgroud)

预期输出(终端):

Mumbai
Rain
Run Code Online (Sandbox Code Playgroud)

实际输出(终端):https : …

android future async-await dart flutter

5
推荐指数
1
解决办法
3238
查看次数

在清单 [Android] 中找不到权限

我一直在尝试各种方法来调用我的位置数据,从 IP 位置 API 到 Flutter 插件,但都无法获得其中任何一个的准确位置数据。通过所有我已经解决了Geolocator。但我一直得到

I/flutter ( 5206): null
I/flutter ( 5206): GeolocationStatus.unknown
Run Code Online (Sandbox Code Playgroud)

在我的 Init 状态下调用它时,如下所示:

  void initState() {
    super.initState();
    fetchData();
  }


  fetchData() async {
    setState(() {
      isLoading = true; //Data is loading
    });
    GeolocationStatus geolocationStatus =
        await Geolocator().checkGeolocationPermissionStatus();
    position = await Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    if (position != null) {
      lastPosition = await Geolocator()
          .getLastKnownPosition(desiredAccuracy: LocationAccuracy.high);
    }

    ...

    if (position != null)
      print(position);
    else
      print(lastPosition);

    print(geolocationStatus);

    setState(() {
      isLoading = false; //Data has loaded
    });
  } …
Run Code Online (Sandbox Code Playgroud)

xml android flutter

1
推荐指数
1
解决办法
5392
查看次数

标签 统计

android ×2

flutter ×2

async-await ×1

dart ×1

future ×1

xml ×1