我想使用StreamSubscription
连续跟踪用户的距离,但是每次应用崩溃时。用于定位用户的插件为location
。这是代码。
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:location/location.dart';
import 'package:latlong/latlong.dart' as LatLong;
const LatLng defaultPosition = LatLng(0.906706, 123.259054);
class MapRoute extends StatefulWidget {
final LatLng lokasiKegiatan;
final double maxPerimeter;
MapRoute(this.lokasiKegiatan, this.maxPerimeter);
@override
_MapRouteState createState() => _MapRouteState();
}
class _MapRouteState extends State<MapRoute> {
Completer<GoogleMapController> mapController = Completer();
bool isLoading = false;
bool isUserNearby = false;
bool isUserVisible = true;
Location _locationManager = new Location();
StreamSubscription<LocationData> _locationTrackerSub;
LocationData _lokasiTerkini;
String errorMessage;
double distanceToLocation;
@override
void initState() {
// TODO: implement initState
super.initState();
_locationTrackerSub = _locationManager.onLocationChanged().listen((data) {
setState(() {
_lokasiTerkini = data;
});
});
}
@override
void dispose() {
// TODO: implement dispose
_locationTrackerSub = null;
super.dispose();
}
@override
Widget build(BuildContext context) {
MarkerId markerId = MarkerId('ACARA PEMDA');
Marker markerLokasi = new Marker(
markerId: markerId,
position: widget.lokasiKegiatan,
infoWindow: InfoWindow(
title: 'Apel Bersama',
snippet: 'Apel Bersama PEMDA BOLMUT',
),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed));
final Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
markers[markerId] = markerLokasi;
return Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Padding(
padding: EdgeInsets.all(3.0),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(30.0)),
child: Column(
children: <Widget>[
SizedBox(
height: 400,
child: Stack(
children: <Widget>[
GoogleMap(
mapType: MapType.normal,
onMapCreated: _onMapCreated,
myLocationEnabled: isUserVisible,
initialCameraPosition: CameraPosition(
target: defaultPosition,
zoom: 15,
),
markers: Set<Marker>.of(markers.values),
),
Positioned(
top: 12,
left: 12,
child: SizedBox(
width: 39,
height: 37,
child: RaisedButton(
elevation: 1.0,
padding: EdgeInsets.all(2.0),
shape: RoundedRectangleBorder(
side: BorderSide(
width: 0.5,
color: Colors.white,
),
borderRadius: BorderRadius.all(
Radius.circular(5.0),
),
),
child: Icon(
Icons.location_on,
color: Colors.black54,
size: 27,
),
color: Colors.white,
onPressed: _goToLokasiKegiatan,
),
),
),
Positioned(
top: 25,
left: 57,
child: distanceToLocation != null
? Text('0 KM')
: Text(
'menghitung jarak ke lokasi kegiatan...',
style: ThemeData().textTheme.title,
),
)
],
),
),
],
),
),
),
SizedBox(
height: 20.0,
),
RaisedButton(
color: Colors.blue,
shape:
CircleBorder(side: BorderSide(width: 1.0, color: Colors.amber)),
elevation: 5,
child: Icon(
Icons.fingerprint,
color: Colors.white,
size: 74,
),
onPressed: isUserNearby ? () {} : null)
],
);
}
void _onMapCreated(GoogleMapController controller) async {
mapController.complete(controller);
_goToLokasiKegiatan();
}
void _goToLokasiKegiatan() async {
mapController.future.then((controller) {
controller.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(target: widget.lokasiKegiatan, zoom: 18.0),
),
);
});
}
double calculateUserDistance(LocationData currentPosition) {
//converting from gmap to latlong package
var lat = widget.lokasiKegiatan.latitude;
var long = widget.lokasiKegiatan.longitude;
var ulat = currentPosition.latitude;
var ulong = currentPosition.longitude;
var jarak = LatLong.Distance();
double meter = jarak.as(
LatLong.LengthUnit.Meter,
new LatLong.LatLng(lat, long),
new LatLong.LatLng(ulat, ulong),
);
return meter;
}
void _processUserLocation(LocationData locationData) async {
var distanceToLocation = calculateUserDistance(locationData);
if (distanceToLocation <= widget.maxPerimeter) {
setState(() {
isUserNearby = true;
});
}
setState(() {
this.distanceToLocation = distanceToLocation;
});
}
void _initLocation() async {
LocationData location;
try {
if (await _locationManager.hasPermission()) {
_locationTrackerSub = _locationManager.onLocationChanged().listen(
(locationData) {
setState(() {
_lokasiTerkini = locationData;
});
},
);
}
} catch (e) {
debugPrint(e);
}
}
}
和调试输出。
Launching lib\main.dart on ASUS X00QD in debug mode...
Built build\app\outputs\apk\debug\app-debug.apk.
I/SimplePermission(16555): Checking permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
Reloaded 2 of 449 libraries in 1,234ms.
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
Reloaded 2 of 449 libraries in 951ms.
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 2
I/SimplePermission(16555): Requesting permission : android.permission.ACCESS_FINE_LOCATION
I/SimplePermission(16555): Requesting permission status : 3
I/zzbz (16555): Making Creator dynamically
W/mple.absensigp(16555): Unsupported class loader
W/mple.absensigp(16555): Skipping duplicate class check due to unsupported classloader
I/DynamiteModule(16555): Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:221
I/DynamiteModule(16555): Selected remote version of com.google.android.gms.maps_dynamite, version >= 221
V/DynamiteModule(16555): Dynamite loader version >= 2, using loadModule2NoCrashUtils
W/mple.absensigp(16555): Unsupported class loader
W/mple.absensigp(16555): Skipping duplicate class check due to unsupported classloader
I/Google Maps Android API(16555): Google Play services client version: 12451000
I/Google Maps Android API(16555): Google Play services package version: 15090039
W/mple.absensigp(16555): Accessing hidden field Ljava/nio/Buffer;->address:J (light greylist, reflection)
D/NetworkSecurityConfig(16555): No Network Security Config specified, using platform default
I/DpmTcmClient(16555): RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
W/DynamiteModule(16555): Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule(16555): Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
I/DynamiteModule(16555): Selected remote version of com.google.android.gms.googlecertificates, version >= 4
W/mple.absensigp(16555): Unsupported class loader
W/mple.absensigp(16555): Skipping duplicate class check due to unsupported classloader
I/Adreno (16555): DequeueBuffer: dequeueBuffer failed
I/Adreno (16555): DequeueBuffer: dequeueBuffer failed
I/Adreno (16555): DequeueBuffer: dequeueBuffer failed
W/OpenGLRenderer(16555): swapBuffers encountered EGL error 12301 on 0x75f6b20a00, halting rendering...
I/Google Maps Android API(16555): Google Play services package version: 15090039
W/System (16555): A resource failed to call release.
I/Google Maps Android API(16555): Google Play services package version: 15090039
I/Google Maps Android API(16555): Google Play services package version: 15090039
W/System (16555): A resource failed to call release.
W/System (16555): A resource failed to call release.
I/Google Maps Android API(16555): Google Play services package version: 15090039
W/System (16555): A resource failed to call release.
I/Google Maps Android API(16555): Google Play services package version: 15090039
I/mple.absensigp(16555): Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
W/System (16555): A resource failed to call release.
Reloaded 3 of 449 libraries in 1,114ms.
I/Adreno (16555): DequeueBuffer: dequeueBuffer failed
I/Adreno (16555): DequeueBuffer: dequeueBuffer failed
I/Adreno (16555): DequeueBuffer: dequeueBuffer failed
W/OpenGLRenderer(16555): swapBuffers encountered EGL error 12301 on 0x75ef08a900, halting rendering...
E/flutter (16555): [ERROR:flutter/shell/platform/android/platform_view_android_jni.cc(40)] java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/app/ActivityCompat;
E/flutter (16555): at com.lyokone.location.LocationPlugin.checkPermissions(LocationPlugin.java:186)
E/flutter (16555): at com.lyokone.location.LocationPlugin.onListen(LocationPlugin.java:271)
E/flutter (16555): at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onListen(EventChannel.java:181)
E/flutter (16555): at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onMessage(EventChannel.java:160)
E/flutter (16555): at io.flutter.view.FlutterNativeView$PlatformMessageHandlerImpl.handleMessageFromDart(FlutterNativeView.java:188)
E/flutter (16555): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:202)
E/flutter (16555): at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (16555): at android.os.MessageQueue.next(MessageQueue.java:326)
E/flutter (16555): at android.os.Looper.loop(Looper.java:163)
E/flutter (16555): at android.app.ActivityThread.main(ActivityThread.java:6732)
E/flutter (16555): at java.lang.reflect.Method.invoke(Native Method)
E/flutter (16555): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/flutter (16555): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
E/flutter (16555): Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.core.app.ActivityCompat" on path: DexPathList[[zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.example.absensigps-vaz4urV31quLBNjs6K8-IA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.absensigps-vaz4urV31quLBNjs6K8-IA==/lib/arm64, /data/app/com.example.absensigps-vaz4urV31quLBNjs6K8-IA==/base.apk!/lib/arm64-v8a, /system/lib64]]
E/flutter (16555): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
E/flutter (16555): at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
E/flutter (16555): at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
E/flutter (16555): ... 13 more
E/flutter (16555):
F/flutter (16555): [FATAL:flutter/shell/platform/android/platform_view_android_jni.cc(77)] Check failed: CheckException(env).
F/libc (16555): Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 16555 (mple.absensigps), pid 16555 (mple.absensigps)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'asus/WW_X00QD/ASUS_X00QD:9/PPR1.180610.009/16.0611.1901.1-0:user/release-keys'
Revision: '0'
ABI: 'arm64'
pid: 16555, tid: 16555, name: mple.absensigps >>> com.example.absensigps <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: '[FATAL:flutter/shell/platform/android/platform_view_android_jni.cc(77)] Check failed: CheckException(env).
'
x0 0000000000000000 x1 00000000000040ab x2 0000000000000006 x3 0000000000000008
x4 0000000000000000 x5 0000000000000000 x6 0000000000000000 x7 0000008000000000
x8 0000000000000083 x9 afcf0ea64e096394 x10 0000000000000000 x11 fffffffc7ffffbdf
x12 0000000000000001 x13 0000000000000018 x14 ffffffffffffffff x15 0000f282b913c801
x16 00000076978b02c8 x17 00000076977ee0d0 x18 0000000000000001 x19 00000000000040ab
x20 00000000000040ab x21 0000007fd63435b8 x22 00000000000000c3 x23 0000000000000099
x24 00000075f8bbdfc0 x25 0000000000000050 x26 00000075eb83a6a0 x27 0000007612cf7c88
x28 0000000000000034 x29 0000007fd63435a0
sp 0000007fd6343560 lr 00000076977e2bfc pc 00000076977e2c24
backtrace:
#00 pc 0000000000021c24 /system/lib64/libc.so (abort+116)
#01 pc 00000000006b7e48 /data/app/com.example.absensigps-vaz4urV31quLBNjs6K8-IA==/lib/arm64/libflutter.so (offset 0x690000)
#02 pc 00000000006aca1c /data/app/com.example.absensigps-vaz4urV31quLBNjs6K8-IA==/lib/arm64/libflutter.so (offset 0x690000)
#03 pc 00000000006abc74 /data/app/com.example.absensigps-vaz4urV31quLBNjs6K8-IA==/lib/arm64/libflutter.so (offset 0x690000)
#04 pc 00000000006e5018 /data/app/com.example.absensigps-vaz4urV31quLBNjs6K8-IA==/lib/arm64/libflutter.so (offset 0x690000)
#05 pc 00000000006b9368 /data/app/com.example.absensigps-vaz4urV31quLBNjs6K8-IA==/lib/arm64/libflutter.so (offset 0x690000)
#06 pc 00000000006baf58 /data/app/com.example.absensigps-vaz4urV31quLBNjs6K8-IA==/lib/arm64/libflutter.so (offset 0x690000)
#07 pc 00000000000141c0 /system/lib64/libutils.so (android::Looper::pollInner(int)+856)
#08 pc 0000000000013dcc /system/lib64/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+60)
#09 pc 0000000000120a94 /system/lib64/libandroid_runtime.so (android::android_os_MessageQueue_nativePollOnce(_JNIEnv*, _jobject*, long, int)+44)
#10 pc 00000000003e4fec /system/framework/arm64/boot-framework.oat (offset 0x3d0000) (android.media.MediaExtractor.seekTo [DEDUPED]+140)
#11 pc 000000000000284c /dev/ashmem/dalvik-jit-code-cache (deleted) (android.os.MessageQueue.next+204)
#12 pc 000000000006c8d4 /dev/ashmem/dalvik-jit-code-cache (deleted) (android.os.Looper.loop+404)
#13 pc 0000000000554cdc /system/lib64/libart.so (art_quick_osr_stub+44)
#14 pc 0000000000306f9c /system/lib64/libart.so (art::jit::Jit::MaybeDoOnStackReplacement(art::Thread*, art::ArtMethod*, unsigned int, int, art::JValue*)+1996)
#15 pc 000000000052ad1c /system/lib64/libart.so (MterpMaybeDoOnStackReplacement+144)
#16 pc 000000000054b9f0 /system/lib64/libart.so (ExecuteMterpImpl+33136)
#17 pc 0000000000b15512 /system/framework/boot-framework.vdex (android.os.Looper.loop+986)
#18 pc 0000000000252fc0 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.3783761849+488)
#19 pc 0000000000258ab4 /system/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+216)
#20 pc 00000000002792a0 /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+940)
#21 pc 0000000000525c14 /system/lib64/libart.so (MterpInvokeStatic+204)
#22 pc 0000000000547194 /system/lib64/libart.so (ExecuteMterpImpl+14612)
#23 pc 00000000003919ec /system/framework/boot-framework.vdex (android.app.ActivityThread.main+256)
#24 pc 0000000000252fc0 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.3783761849+488)
#25 pc 0000000000514fa4 /system/lib64/libart.so (artQuickToInterpreterBridge+1020)
#26 pc 000000000055dafc /system/lib64/libart.so (art_quick_to_interpreter_bridge+92)
#27 pc 0000000000554c4c /system/lib64/libart.so (art_quick_invoke_static_stub+604)
#28 pc 00000000000cf6e8 /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+232)
#29 pc 000000000045c840 /system/lib64/libart.so (art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, art::(anonymous namespace)::ArgArray*, art::JValue*, char const*)+104)
#30 pc 000000000045e294 /system/lib64/libart.so (art::InvokeMethod(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jobject*, _jobject*, unsigned long)+1440)
#31 pc 00000000003ee1d4 /system/lib64/libart.so (art::Method_invoke(_JNIEnv*, _jobject*, _jobject*, _jobjectArray*)+52)
#32 pc 000000000011e6d4 /system/framework/arm64/boot-core-oj.oat (offset 0x114000) (java.lang.Class.getDeclaredMethodInternal [DEDUPED]+180)
#33 pc 0000000000554988 /system/lib64/libart.so (art_quick_invoke_stub+584)
#34 pc 00000000000cf6c8 /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+200)
#35 pc 000000000027f2b4 /system/lib64/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::ShadowFrame*, unsigned short, art::JValue*)+344)
#36 pc 00000000002792bc /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+968)
#37 pc 0000000000524710 /system/lib64/libart.so (MterpInvokeVirtual+588)
#38 pc 0000000000547014 /system/lib64/libart.so (ExecuteMterpImpl+14228)
#39 pc 0000000000c3779e /system/framework/boot-framework.vdex (com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run+22)
#40 pc 0000000000252fc0 /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.3783761849+488)
#41 pc 0000000000514fa4 /system/lib64/libart.so (artQuickToInterpreterBridge+1020)
#42 pc 000000000055dafc /system/lib64/libart.so (art_quick_to_interpreter_bridge+92)
#43 pc 0000000000bf9920 /system/framework/arm64/boot-framework.oat (offset 0x3d0000) (com.android.internal.os.ZygoteInit.main+3088)
#44 pc 0000000000554c4c /system/lib64/libart.so (art_quick_invoke_static_stub+604)
#45 pc 00000000000cf6e8 /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+232)
#46 pc 000000000045c840 /system/lib64/libart.so (art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, art::(anonymous namespace)::ArgArray*, art::JValue*, char const*)+104)
#47 pc 000000000045c4a0 /system/lib64/libart.so (art::InvokeWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jmethodID*, std::__va_list)+424)
#48 pc 0000000000361b60 /system/lib64/libart.so (art::JNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, std::__va_list)+652)
#49 pc 00000000000b2e24 /system/lib64/libandroid_runtime.so (_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+120)
#50 pc 00000000000b57a8 /system/lib64/libandroid_runtime.so (android::AndroidRuntime::start(char const*, android::Vector<android::String8> const&, bool)+756)
#51 pc 00000000000022f8 /system/bin/app_process64 (main+1380)
#52 pc 00000000000aca4c /system/lib64/libc.so (__libc_init+88)
Lost connection to device.
Exited (sigterm)
感谢您帮助菜鸟。