コードは位置を要求し、ユーザーは承認し、コードはエラーをスローします:
PlatformException(PERMISSION_DENIED、位置データへのアクセスが拒否されました、null)
しかし、もう一度位置を要求した後、ダイアログやエラーなしで正しい位置が即座に返されます。
位置が返され、位置とユーザーの承認を求めるときにエラーはスローされません。
import 'package:geolocator/geolocator.dart';
try{
position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
catch(error)
{
print("error in geolocation: $error"); //error in geolocation: PlatformException(PERMISSION_DENIED, Access to location data denied, null)
print("position: $position"); // position: null
print("retrying"); // retrying
try{
position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
catch(error)
{
await printGeoStatus(5);
print("2nd error in geolocation $error");
}
print("final position: $position"); //flutter: final position: Lat: 59.xyz, Long: 17.xyz
}
Flutterバージョン: 「Channeldev、v1.5.8」
ジオロケーターバージョン: ^ 4.0.2
プラットホーム:
@FlashJonas最近、 geolocator
プラグイン(バージョン5.0.0)を完全にリファクタリングしましたが、最新バージョンでもこの問題が発生するのではないかと思いました。
@mvanbeusekom問題が5.0.0でもまだ存在することを確認します
AppBreweryコースでパッケージを見つけました。
パッケージのドキュメントに従うことにし、plistファイルに3つのキーを追加しました。
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>
最初の呼び出し
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
ユーザーに許可を求める(3つの選択肢がある):
「アプリの使用中のみ」を押すと、ターミナルは次のログを記録します。
[VERBOSE-2:ui_dart_state.cc(148)]未処理の例外:PlatformException(PERMISSION_DENIED、位置データへのアクセスが拒否されました、null)
0 Geolocator._handleInvalidPermissions(package:geolocator / geolocator.dart:191:7)
1 Geolocator.getCurrentPosition(package:geolocator / geolocator.dart:102:7)
2 _LoadingScreenState.getLocation(package:clima / screens / Loading_screen.dart:13:10)
3_LoadingScreenState.build。
(package:clima / screens / loading_screen.dart:23:13) 4 _InkResponseState._handleTap(package:flutter / src / material / ink_well.dart:511:14)
5_InkResponseState.build。
(package:flutter / src / material / ink_well.dart:566:30) 6 GestureRecognizer.invokeCallback(package:flutter / src / gestures / recognizer.dart:166:24)
7 TapGestureRecognizer._checkUp(package:flutter / src / gestures / tap.dart:240:9)
8 TapGestureRecognizer.handlePrimaryPointer(package:flutter / src / gestures / tap.dart:177:9)
9 <…>
2回目のプレスは問題なく、位置は問題なく記録されます。
フラッター:緯度:37.785834、ロング:-122.406417
コード:
geolocator 5.1.2を使用している私のiPhone5s、12.2でも同じ問題があります。
*編集(Flutter Channel dev、v1.8.4)
// Called after closing welcome alert
Future getFirstLocation() async {
Position position;
try {
position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
} catch (error) {
print("getfirstlocation error 1");
try {
position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
} catch (error) {
print("error in geolocation $error");
}
}
print("getfirstlocation position: $position");
if (position != null) {
// send to database
}
}
上記のコードの実行からのログ:
flutter: getfirstlocation error 1
flutter: getfirstlocation position: Lat: 59.xxxx, Long: 17.yyyy
私は同じエラーに直面しています:
Android(Oneplus 2)-Android 8.1.0-Geolocator 5.1.2
(場所の許可が与えられます)
2019-08-15 16:54:22.606 926-1110/system_process E/InputDispatcher: channel '569829a de.paschke.coffeebreak/de.paschke.coffeebreak.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-08-15 16:54:56.808 14735-14766/? E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(PERMISSION_DENIED, Access to location data denied, null)
#0 Geolocator._handleInvalidPermissions (package:geolocator/geolocator.dart:207:7)
#1 Geolocator.getLastKnownPosition (package:geolocator/geolocator.dart:146:7)
<asynchronous suspension>
#2 LocationService.getLastKnownLocation (package:coffee_break/services/location_service.dart:24:51)
<asynchronous suspension>
#3 UserStoreBase.checkLastLocation (package:coffee_break/stores/user_store.dart:125:36)
3つの権限文字列すべてを使用すると、同じエラーが発生しました。 NSLocationWhenInUseUsageDescriptionのみを使用すると、アプリは再び機能します。 私の推測では、3つの権限すべてを使用すると、アプリが起動し、ユーザーが確認を求める前に場所を尋ねます。 UIの動作はそれを確認しているようですが、それは単なる推測です。 とにかく、NSLocationWhenInUseUsageDescriptionだけで生活できる場合は、他の2つを削除してください。
同じ問題:(最後のバージョンのフラッターの安定したチャネル。
このパッケージは最悪です...
同じエラー、Android10の最新バージョンのフラッター。
私は同じ問題を抱えています!
この問題は解決されるのだろうか、私はそれが1年以上前のものであり、人々はまだそれに直面していると思います。 最近、私はFlutterの学習を開始し、同じ問題に遭遇しました。再現する手順はまったく同じです。
iOSプロンプトから[アプリの使用中のみ]オプションを選択すると、アプリから2回目に場所をリクエストしても、正しい場所データを取得しても、フラッターコンソールで例外が発生します。
最新の5.3.2+2
バージョンを使用するPS
私は現在、これと他の問題のリストの修正に取り組んでいます(詳細については、#430を参照してください)。 ただし、メジャーアップデートになります。
現在、iOS側は準備ができており、Android側で動作するようになります。
これはバージョン6.0.0で修正されるはずです
返信が遅れる可能性がありますが、アプリがiOSで許可を求めておらず、Androidでも完全に正常に機能しているという同じ問題に直面していました。
許可を求められなかったため、許可コードがiOSで機能しませんでした。 手動で許可を求めるために使用できる「location_permissions」という名前のパッケージを見つけました。
手順は次のとおりです
パッケージをファイルにインポートします
import'package :location_permissions / location_permissions.dart ';
許可を求めるページに次のコードを追加します。 (アプリの最初のページに追加することをお勧めします。)
<strong i="16">@override</strong>
void initState() {
....
if (Platform.isIOS) {
location_permission();
}
....
}
同じファイルに次の2つのメソッドを追加します
void location_permission()async {
最終的なPermissionStatusパーミッション= await _getLocationPermission();
if(permission == PermissionStatus.granted){
最終位置= geolocator.getCurrentPosition(を待つ
DesiredAccuracy:LocationAccuracy.best);
// Use the position to do whatever...
}
}
未来
最終的なPermissionStatusパーミッション= LocationPermissions()を待つ
.checkPermissionStatus(level:LocationPermissionLevel.location);
if (permission != PermissionStatus.granted) {
final PermissionStatus permissionStatus = await LocationPermissions()
.requestPermissions(
permissionLevel: LocationPermissionLevel.location);
return permissionStatus;
} else {
return permission;
}
}
これで、iOSアプリに場所の許可を求めるポップアップが表示されるはずです。
最も参考になるコメント
私は現在、これと他の問題のリストの修正に取り組んでいます(詳細については、#430を参照してください)。 ただし、メジャーアップデートになります。
現在、iOS側は準備ができており、Android側で動作するようになります。