使用或覆盖已弃用的 API。注意:使用 -Xlint:deprecation 重新编译以获取详细信息

uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details

提问人:Albani Kren 提问时间:11/16/2023 最后编辑:Albani Kren 更新时间:11/16/2023 访问量:81

问:

我从天气提供商网站调用了 API 数据,我还使用了 Flutter 的地理编码包,但是当我尝试运行该应用程序时出现错误,提示:

Note: C:\Users\<my-PC-name>\AppData\Local\Pub\Cache\hosted\pub.dev\geocoding_android-2.1.2\android\src\main\java\com\baseflow\geocoding\Geocoding.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.

debug 仍在运行,但 API 数据未检索,应用程序未显示错误,上面我假设地理编码文件存在问题 & 地理编码文件包含

class Geocoding {
    private final Context androidContext;

    /**
     * Uses the given {@code androidContext} to execute geocoding features
     *
     * @param androidContext the context to use when requesting geocoding features
     */
    Geocoding(Context androidContext) {
        this.androidContext = androidContext;
    }

    /**
     * Returns a list of Address objects matching the supplied address string.
     *
     * @param address the address string for the search
     * @param locale the desired Locale for the query results
     * @return a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
     * @throws java.io.IOException if the network is unavailable or any other I/O problem occurs.
     */
    List<Address> placemarkFromAddress(String address, Locale locale) throws IOException {

        final Geocoder geocoder = createGeocoder(androidContext, locale);
        return geocoder.getFromLocationName(address, 5);
    }

    /**
     * Returns a list of Address objects matching the supplied coordinates.
     *
     * @param latitude the latitude point for the search
     * @param longitude the longitude point for the search
     * @param locale the desired Locale for the query results
     * @return a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
     * @throws IOException if the network is unavailable or any other I/O problem occurs.
     */
    List<Address> placemarkFromCoordinates(
            double latitude,
            double longitude,
            Locale locale) throws IOException {

        final Geocoder geocoder = createGeocoder(androidContext, locale);
        return geocoder.getFromLocation(latitude, longitude, 5);
    }

    private static Geocoder createGeocoder(Context androidContext, @Nullable Locale locale) {
        return (locale != null)
                ? new Geocoder(androidContext, locale)
                : new Geocoder(androidContext);
    }
}

我不明白 Xlint 的含义,但在此之前我在 '' 中添加了一个 '' 沙耶·梅南巴坎uses-permissionandroid/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

这用于提供位置访问,稍后将由用户批准,因为应用程序需要位置访问

我还创建了一个服务,稍后将检索 API 数据并将其存储在 JSON 文件中,如下所示

import 'dart:convert';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:myapp/models/weather_model.dart';
import 'package:http/http.dart' as http;

class WeatherService {
  static const BASE_URL = 'http://api.openweathermap.org/data/2.5/weather';
  final String apikey;

  WeatherService(this.apikey);

  Future<Weather> getWeather(String cityName) async {
    final response = await http
        .get(Uri.parse('$BASE_URL?q=$cityName&appid=$apikey&units=metrics'));

    if (response.statusCode == 200) {
      return Weather.fromJson(jsonDecode(response.body));
    } else {
      throw Exception("Failed to load weather");
    }
  }

  Future<String> getCurrentCity() async {
    LocationPermission permission = await Geolocator.checkPermission();

    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
    }

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

    List<Placemark> Placemarks =
        await placemarkFromCoordinates(position.latitude, position.longitude);

    String? city = Placemarks[0].locality;

    return city ?? '';
  }
}

这里我用openweathermap作为API服务商,我的代码有没有问题,怎么解决问题,谢谢

可以显示已成功调用的 API 数据,并可以解决运行代码时出现的问题

XML Flutter 飞镖

评论


答: 暂无答案