質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Q&A

解決済

1回答

4007閲覧

flutter geolocationの許可のエラーを解決したい

t_msda

総合スコア30

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

0グッド

0クリップ

投稿2022/01/05 04:12

前提・実現したいこと

geolocationを用いて、現在地の緯度経度を取得しようとしています。
エラーを読んだ限りデバイスの許可が出ていないということを把握しました。
そのため、ドキュメント通りにios,androidのinfo.splitなどに記載しましたが解決しません。

発生している問題・エラーメッセージ

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: User denied permissions to access the device's location. #0 MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:127:7) <asynchronous suspension> #1 _MyHomePageState.getLocation (package:applicationname/main.dart:37:25) <asynchronous suspension>

該当のソースコード

とりあえず、geolocationから緯度経度を取得するようにしています。

//main.dart import 'package:flutter/material.dart'; import 'package:geolocator/geolocator.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key, required this.title}) : super(key: key); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { String _location = "no data"; Future<void> getLocation() async { // 現在の位置を返す Position position = await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.high); setState(() { _location = position.toString(); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(_location), ), body: Center( // Center is a layout widget. It takes a single child and positions it // in the middle of the parent. child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text( 'You have pushed the button this many times:', ), Text( '_location', style: Theme.of(context).textTheme.headline4, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: getLocation, child: Icon(Icons.location_on)), ); } }

info.split

1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 3<plist version="1.0"> 4<dict> 5 <key>NSLocationWhenInUseUsageDescription</key> 6 <string>This app needs access to location when open.</string> 7 <key>NSLocationAlwaysUsageDescription</key> 8 <string>This app needs access to location when in the background.</string> 9 <key>CFBundleDevelopmentRegion</key> 10 <string>$(DEVELOPMENT_LANGUAGE)</string> 11 <key>CFBundleDisplayName</key> 12 <string>applicationname</string> 13 <key>CFBundleExecutable</key> 14 <string>$(EXECUTABLE_NAME)</string> 15 <key>CFBundleIdentifier</key> 16 <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> 17 <key>CFBundleInfoDictionaryVersion</key> 18 <string>6.0</string> 19 <key>CFBundleName</key> 20 <string>applicationname</string> 21 <key>CFBundlePackageType</key> 22 <string>APPL</string> 23 <key>CFBundleShortVersionString</key> 24 <string>$(FLUTTER_BUILD_NAME)</string> 25 <key>CFBundleSignature</key> 26 <string>????</string> 27 <key>CFBundleVersion</key> 28 <string>$(FLUTTER_BUILD_NUMBER)</string> 29 <key>LSRequiresIPhoneOS</key> 30 <true/> 31 <key>UILaunchStoryboardName</key> 32 <string>LaunchScreen</string> 33 <key>UIMainStoryboardFile</key> 34 <string>Main</string> 35 <key>UISupportedInterfaceOrientations</key> 36 <array> 37 <string>UIInterfaceOrientationPortrait</string> 38 <string>UIInterfaceOrientationLandscapeLeft</string> 39 <string>UIInterfaceOrientationLandscapeRight</string> 40 </array> 41 <key>UISupportedInterfaceOrientations~ipad</key> 42 <array> 43 <string>UIInterfaceOrientationPortrait</string> 44 <string>UIInterfaceOrientationPortraitUpsideDown</string> 45 <string>UIInterfaceOrientationLandscapeLeft</string> 46 <string>UIInterfaceOrientationLandscapeRight</string> 47 </array> 48 <key>UIViewControllerBasedStatusBarAppearance</key> 49 <true/> 50</dict> 51</plist> 52

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

あなたのコードのgetLocationメソッドに、以下を追記します。

dart

1 Future<void> getLocation() async { 2 3 // --- 追記 --- 4 LocationPermission permission = await Geolocator.checkPermission(); 5 if (permission == LocationPermission.denied) { 6 permission = await Geolocator.requestPermission(); 7 if (permission == LocationPermission.denied) { 8 return Future.error('Location permissions are denied'); 9 } 10 } 11 // -- 追記おわり -- 12 13 14 // 現在の位置を返す 15 Position position = await Geolocator.getCurrentPosition( 16 desiredAccuracy: LocationAccuracy.high); 17 setState(() { 18 _location = position.toString(); 19 }); 20 }

これでAndroidエミュレータ上では動きました。

※manifestに権限は足している前提です。

AndroidManifest.xml

xml

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

投稿2022/01/08 02:33

umau

総合スコア805

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問