You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter项目geolocator依赖构建报错:LocationRequest.Builder找不到

解决Flutter中geolocator_android编译错误:cannot find symbol LocationRequest.Builder

问题原因

你遇到的编译错误是因为geolocator_android:4.2.2使用了Google Play Services Location库的新API(LocationRequest.Builder),但你的项目依赖中拉取的是旧版本的该库,旧版本里没有这个Builder类,导致编译失败。同时你项目中引入的location: ^4.4.0可能也依赖了旧版本的Play Services Location,进一步加剧了版本冲突。

解决方案

1. 强制指定Google Play Services Location版本

在你的Flutter项目的android/build.gradle文件中,找到allprojects块,添加版本强制策略:

allprojects {
    repositories {
        google()
        mavenCentral()
    }
    // 添加下面这段配置
    configurations.all {
        resolutionStrategy {
            force 'com.google.android.gms:play-services-location:21.0.1'
        }
    }
}

这个配置会强制项目所有依赖使用指定版本的Play Services Location,该版本包含所需的LocationRequest.Builder类。

2. 清理缓存并重建项目

执行以下命令彻底清理并重新获取依赖:

flutter clean
flutter pub get

如果还是不行,可以手动删除android/.gradle文件夹(注意备份),再重新执行上述命令。

3. 移除冲突的依赖(可选)

如果你不需要location: ^4.4.0这个包,可以直接从pubspec.yaml中删除它——两个包都是处理定位的,同时引入大概率会有版本冲突问题。

4. 升级geolocator到最新稳定版

如果上述方法无效,可以尝试将geolocator升级到最新稳定版本,新版本通常会更好地处理依赖兼容性:
修改pubspec.yaml中的geolocator版本:

geolocator: ^latest_version

然后执行flutter pub get更新依赖。

内容的提问来源于stack exchange,提问作者DineshkumarBalan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 07:25:38