Re: MapBox package Flutter: How should we wait/know if "onStyleLoadedCallback" is finished when initialising the map? - flutter

Map shows fine, but does not show marker.
When you add a new marker when map finished loading and idling, the marker shows.
When you add a delay to addSymbol inside "onMapCreated" like below, it then shows the symbol:
Future.delayed(Duration(milliseconds: 1500), () {
mapController.addSymbol(
SymbolOptions(
geometry: LatLng(widget.post.lat, widget.post.long),
iconImage: "airport-15",
iconSize: 5,
),
);
});
How do we know or are suppose to handle the wait for onStyleCallBack?
This is the error that comes up when you don't add the delay:
Attempt to invoke virtual method 'java.util.List com.mapbox.mapboxsdk.plugins.annotation.SymbolManager.create(java.util.List)' on a null object reference
Works perfectly on iOS without any delays.
Same behaviour on device and emulator.
Version: mapbox_gl: ^0.9.0
Code:
onMapCreated: (MapboxMapController controller) async {
controller.addSymbol(
SymbolOptions(
geometry: LatLng(widget.post.lat, widget.post.long),
iconImage: "airport-15",
iconSize: 5,
),
);
},
Full output:
E/MapboxMapBuilder(11157): setCameraTargetBounds is supported only after map initiated.
I/Mbgl-EGLConfigChooser(11157): In emulator: true
D/HostConnection(11157): HostConnection::get() New Host Connection established 0xdfd4d000, enter code heretid 14985
D/HostConnection(11157): HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_0
D/EGL_emulation(11157): eglMakeCurrent: 0xdfd1aae0: ver 3 0 (tinfo 0xc70ab560)
D/eglCodecCommon(11157): setVertexArrayObject: set vao to 0 (0) 0 0
D/EGL_emulation(11157): eglCreateContext: 0xdfd1b740: maj 3 min 0 rcv 3
D/EGL_emulation(11157): eglMakeCurrent: 0xdfd1b740: ver 3 0 (tinfo 0xdfdc3920)
D/eglCodecCommon(11157): setVertexArrayObject: set vao to 0 (0) 3 0
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): Failed to handle method call
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.mapbox.mapboxsdk.plugins.annotation.SymbolManager.create(java.util.List)' on a null object reference
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at com.mapbox.mapboxgl.MapboxMapController.onMethodCall(MapboxMapController.java:648)
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at android.app.ActivityThread.main(ActivityThread.java:7356)
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#plugins.flutter.io/mapbox_maps_5(11157): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/flutter (11157): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'java.util.List com.mapbox.mapboxsdk.plugins.annotation.SymbolManager.create(java.util.List)' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.mapbox.mapboxsdk.plugins.annotation.SymbolManager.create(java.util.List)' on a null object reference
E/flutter (11157): at com.mapbox.mapboxgl.MapboxMapController.onMethodCall(MapboxMapController.java:648)
E/flutter (11157): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter (11157): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/flutter (11157): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/flutter (11157): at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (11157): at android.os.MessageQueue.next(MessageQueue.java:336)
E/flutter (11157): at android.os.Looper.loop(Looper.java:174)
E/flutter (11157): at android.app.ActivityThread.main(ActivityThread.java:7356)
E/flutter (11157): at java.lang.reflect.Method.invoke(Native Method)
E/flutter (11157): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)

Please note: you should only add annotations (e.g. symbols or circles) after onStyleLoadedCallback has been called. So you should add symbols inside onStyleLoadedCallback callback.

Maybe is later but I just wanna post answer because there are some few answers about it ...
Like Edwin TriviƱos says, you just need to set your callback on MapBox parameters and create a new function to do whatever you wanna do. For example, in my case:
Mapbox config:
return MapboxMap(
accessToken: MapboxVariables.accessToken,
styleString: "mapbox://styles/your-url-from-mapbox",
onMapCreated: _onMapCreated,
onStyleLoadedCallback: _onStyleLoaded,
zoomGesturesEnabled: true,
myLocationEnabled: true,
trackCameraPosition: true,
myLocationTrackingMode: MyLocationTrackingMode.Tracking,
onUserLocationUpdated: (UserLocation userLocation){
_movingCameraToLocation();
},
initialCameraPosition: const CameraPosition(
target: LatLng(-33.852, 151.211),
zoom: 11.0,
),
);
As you can see, I'm calling onStyleLoadedCallback: _onStyleLoaded, as a callback. An after that just need a basic _onStyleLoaded function, like this:
void _onStyleLoaded() async {
//your code
}

Related

error when using better player in flutter

E/MethodChannel#better_player_channel: Failed to handle method call
java.lang.NullPointerException: MediaSource.Factory#setDrmSessionManagerProvider no longer handles null by instantiating a new DefaultDrmSessionManagerProvider. Explicitly construct and pass an instance in order to retain the old behavior.
at com.google.android.exoplayer2.util.Assertions.checkNotNull(Assertions.java:174)
at com.google.android.exoplayer2.source.ProgressiveMediaSource$Factory.setDrmSessionManagerProvider(ProgressiveMediaSource.java:186)
at com.jhomlala.better_player.BetterPlayer.buildMediaSource(BetterPlayer.kt:430)
at com.jhomlala.better_player.BetterPlayer.setDataSource(BetterPlayer.kt:196)
at com.jhomlala.better_player.BetterPlayerPlugin.setDataSource(BetterPlayerPlugin.kt:277)
at com.jhomlala.better_player.BetterPlayerPlugin.onMethodCall(BetterPlayerPlugin.kt:151)
at com.jhomlala.better_player.BetterPlayerPlugin.onMethodCall(BetterPlayerPlugin.kt:138)
at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
E/flutter: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, MediaSource.Factory#setDrmSessionManagerProvider no longer handles null by instantiating a new DefaultDrmSessionManagerProvider. Explicitly construct and pass an instance in order to retain the old behavior., null, java.lang.NullPointerException: MediaSource.Factory#setDrmSessionManagerProvider no longer handles null by instantiating a new DefaultDrmSessionManagerProvider. Explicitly construct and pass an instance in order to retain the old behavior.
at com.google.android.exoplayer2.util.Assertions.checkNotNull(Assertions.java:174)
at com.google.android.exoplayer2.source.ProgressiveMediaSource$Factory.setDrmSessionManagerProvider(ProgressiveMediaSource.java:186)
at com.jhomlala.better_player.BetterPlayer.buildMediaSource(BetterPlayer.kt:430)
at com.jhomlala.better_player.BetterPlayer.setDataSource(BetterPlayer.kt:196)
at com.jhomlala.better_player.BetterPlayerPlugin.setDataSource(BetterPlayerPlugin.kt:277)
at com.jhomlala.better_player.BetterPlayerPlugin.onMethodCall(BetterPlayerPlugin.kt:151)
at com.jhomlala.better_player.BetterPlayerPlugin.onMethodCall(BetterPlayerPlugin.kt:138)
at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
)
#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653:7)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:296:18)
#2 MethodChannelVideoPlayer.setDataSource (package:better_player/src/video_player/method_channel_video_player.dart:118:5)
#3 VideoPlayerController._setDataSource (package:better_player/src/video_player/video_player.dart:408:5)
#4 BetterPlayerController._setupDataSource (package:better_player/src/core/better_player_controller.dart:444:9)
#5 BetterPlayerController.setupDataSource (package:better_player/src/core/better_player_controller.dart:274:5)
I face this problem when I use better_player in flutter, It was work good but now show this problem .
Any one can help me

Flutter Firebase Machine Learning Custom model

I have a method thats called to load models but seems to crash every time I call it, I followed the documentation for it I believe so I dont understand why.
Future loadModel() async {
await FirebaseModelDownloader.instance
.getModel(
"Breed-Detector",
FirebaseModelDownloadType.latestModel,
FirebaseModelDownloadConditions(
iosAllowsCellularAccess: true,
iosAllowsBackgroundDownloading: false,
androidChargingRequired: false,
androidWifiRequired: false,
androidDeviceIdleRequired: false,
))
.then((customModel) async {
final localModelPath = customModel.file;
Tflite.close();
String res;
res = (await Tflite.loadModel(
model: localModelPath.toString(), labels: "assets/labels.txt"))!;
print("Models Loading status: ${res}");
});
}
I get the error
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Failed to load model, flutter_assets/File: '/data/user/0/com.example.find_my_dog/no_backup/com.google.firebase.ml.custom.models/W0RFRkFVTFRd+MToyNjY1MDIxMjc4ODE6YW5kcm9pZDpjZjhiMjk0NjAwNWRjZGQ1ZjE5M2Q4/Breed-Detector/0', java.io.FileNotFoundException: flutter_assets/File: '/data/user/0/com.example.find_my_dog/no_backup/com.google.firebase.ml.custom.models/W0RFRkFVTFRd+MToyNjY1MDIxMjc4ODE6YW5kcm9pZDpjZjhiMjk0NjAwNWRjZGQ1ZjE5M2Q4/Breed-Detector/0'
but I have the model uploaded
tried uploading a separate model and that seemed to try and download it at first but failed again.
/ModelFileDownloadSer( 7365): Need to download a new model.
D/ModelFileDownloadSer( 7365): Schedule a new downloading task: 64
D/ModelFileDownloadSer( 7365): Model downloaded successfully
D/ModelFileDownloadSer( 7365): Moving downloaded model from external storage to destination folder.
D/ModelFileDownloadSer( 7365): Moved the downloaded model to destination folder successfully: /data/user/0/com.example.find_my_dog/no_backup/com.google.firebase.ml.custom.models/W0RFRkFVTFRd+MToyNjY1MDIxMjc4ODE6YW5kcm9pZDpjZjhiMjk0NjAwNWRjZGQ1ZjE5M2Q4/Dog-Breeds
E/flutter ( 7365): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Failed to load model, flutter_assets/File: '/data/user/0/com.example.find_my_dog/no_backup/com.google.firebase.ml.custom.models/W0RFRkFVTFRd+MToyNjY1MDIxMjc4ODE6YW5kcm9pZDpjZjhiMjk0NjAwNWRjZGQ1ZjE5M2Q4/Dog-Breeds/0', java.io.FileNotFoundException: flutter_assets/File: '/data/user/0/com.example.find_my_dog/no_backup/com.google.firebase.ml.custom.models/W0RFRkFVTFRd+MToyNjY1MDIxMjc4ODE6YW5kcm9pZDpjZjhiMjk0NjAwNWRjZGQ1ZjE5M2Q4/Dog-Breeds/0'
E/flutter ( 7365): at android.content.res.AssetManager.nativeOpenAssetFd(Native Method)
E/flutter ( 7365): at android.content.res.AssetManager.openFd(AssetManager.java:898)
E/flutter ( 7365): at sq.flutter.tflite.TflitePlugin.loadModel(TflitePlugin.java:210)
E/flutter ( 7365): at sq.flutter.tflite.TflitePlugin.onMethodCall(TflitePlugin.java:98)
E/flutter ( 7365): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/flutter ( 7365): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/flutter ( 7365): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$DartMessenger(DartMessenger.java:319)
E/flutter ( 7365): at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$TsixYUB5E6FpKhMtCSQVHKE89gQ.run(Unknown Source:12)
E/flutter ( 7365): at android.os.Handler.handleCallback(Handler.java:938)
E/flutter ( 7365): at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter ( 7365): at android.os.Looper.loop(Looper.java:223)
E/flutter ( 7365): at android.app.ActivityThread.main(ActivityThread.java:7656)
E/flutter ( 7365): at java.lang.reflect.Method.invoke(Native Method)
E/flutter ( 7365): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/flutter ( 7365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter ( 7365): , null)

I am Working on Flutter Foreground task facing an issue of not running code which i have worked can any one help me out to under stand problem

basically i am working on voice application which takes an input as voice and convert voice into text if any command like hello received it will resond with an answer in voice
i have used packages
flutter tts
flutter speach
for foreground i have used
https://medium.com/#burlakovv.ivan/simple-android-foreground-service-in-flutter-for-non-kotlin-developers-31607909a633
here is out put of console
I/flutter ( 2692): AppLifecycleState----------------------------------state: AppLifecycleState.inactive
E/eglCodecCommon( 2692): goldfish_dma_create_region: could not obtain fd to device! fd -1 errno=2
I/flutter ( 2692): AppLifecycleState----------------------------------state: AppLifecycleState.paused
I/flutter ( 2692): AppLifecycleState----------------------------------state: AppLifecycleState.detached
I/flutter ( 2692): false
I/flutter ( 2692): _MyAppState.activateSpeechRecognizer...
W/TextToSpeech( 2692): stop failed: not bound to TTS engine
W/TextToSpeech( 2692): shutdown failed: not bound to TTS engine
I/flutter ( 2692): Marco Has been Found++++++++++++++++++++++++++++++
D/FlutterSpeechPlugin( 2692): Current Locale : en_US
E/MethodChannel#com.flutter.speech_recognition( 2692): Failed to handle method call
E/MethodChannel#com.flutter.speech_recognition( 2692): java.lang.NullPointerException: Attempt to invoke interface method 'android.app.Activity io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding.getActivity()' on a null object reference
E/MethodChannel#com.flutter.speech_recognition( 2692): at com.flutter.speech_recognition.flutter_speech.FlutterSpeechRecognitionPlugin.onMethodCall(FlutterSpeechRecognitionPlugin.java:52)
E/MethodChannel#com.flutter.speech_recognition( 2692): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#com.flutter.speech_recognition( 2692): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#com.flutter.speech_recognition( 2692): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$DartMessenger(DartMessenger.java:319)
E/MethodChannel#com.flutter.speech_recognition( 2692): at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$TsixYUB5E6FpKhMtCSQVHKE89gQ.run(Unknown Source:12)
E/MethodChannel#com.flutter.speech_recognition( 2692): at android.os.Handler.handleCallback(Handler.java:883)
E/MethodChannel#com.flutter.speech_recognition( 2692): at android.os.Handler.dispatchMessage(Handler.java:100)
E/MethodChannel#com.flutter.speech_recognition( 2692): at android.os.Looper.loop(Looper.java:214)
E/MethodChannel#com.flutter.speech_recognition( 2692): at android.app.ActivityThread.main(ActivityThread.java:7356)
E/MethodChannel#com.flutter.speech_recognition( 2692): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#com.flutter.speech_recognition( 2692): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#com.flutter.speech_recognition( 2692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
W/FlutterJNI( 2692): Tried to send a platform message response, but FlutterJNI was detached from native C++. Could not send. Response ID: 15
I/Choreographer( 2692): Skipped 269 frames! The application may be doing too much work on its main thread.
E/MethodChannel#com.flutter.speech_recognition( 2692): Failed to handle method call
E/MethodChannel#com.flutter.speech_recognition( 2692): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.speech.SpeechRecognizer.stopListening()' on a null object reference
E/MethodChannel#com.flutter.speech_recognition( 2692): at com.flutter.speech_recognition.flutter_speech.FlutterSpeechRecognitionPlugin.onMethodCall(FlutterSpeechRecognitionPlugin.java:75)
E/MethodChannel#com.flutter.speech_recognition( 2692): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#com.flutter.speech_recognition( 2692): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#com.flutter.speech_recognition( 2692): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$DartMessenger(DartMessenger.java:319)
E/MethodChannel#com.flutter.speech_recognition( 2692): at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$TsixYUB5E6FpKhMtCSQVHKE89gQ.run(Unknown Source:12)
E/MethodChannel#com.flutter.speech_recognition( 2692): at android.os.Handler.handleCallback(Handler.java:883)
E/MethodChannel#com.flutter.speech_recognition( 2692): at android.os.Handler.dispatchMessage(Handler.java:100)
E/MethodChannel#com.flutter.speech_recognition( 2692): at android.os.Looper.loop(Looper.java:214)
E/MethodChannel#com.flutter.speech_recognition( 2692): at android.app.ActivityThread.main(ActivityThread.java:7356)
E/MethodChannel#com.flutter.speech_recognition( 2692): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#com.flutter.speech_recognition( 2692): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#com.flutter.speech_recognition( 2692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
W/FlutterJNI( 2692): Tried to send a platform message response, but FlutterJNI was detached from native C++. Could not send. Response ID: 16

how to run tensorflow lite movenet lightning model with flutter?

TFLite (movenet/multipose/lightning/tflite/float16)
I have loaded the model successfully but whenever I tried to run it... it just crashes the
app.
here is the code for loading and running the model:
Future loadModel() async {
try {
res = await Tflite.loadModel(
model:
"assets/lite-model_movenet_multipose_lightning_tflite_float16_1.tflite",
);
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> model loaded: " + res.toString());
} catch (e) {
print(e);
print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Failed to load model.');
} }
Future poseNet(File image) async {
int startTime = new DateTime.now().millisecondsSinceEpoch;
var output = await Tflite.runPoseNetOnImage(
threshold: 0.7,
path: image.path,
numResults: 5,
nmsRadius: 15
);
print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> recognitions: ${output}');
setState(() {
_recognitions = output!;
});
int endTime = new DateTime.now().millisecondsSinceEpoch;
print(
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Inference took ${endTime - startTime}ms ");}
here are the logs:
I/tflite (11904): Initialized TensorFlow Lite runtime.
I/flutter (11904): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> model loaded: success
W/System (11904): A resource failed to call close.
D/EGL_emulation(11904): eglCreateContext: 0xead612c0: maj 2 min 0 rcv 2
E/flutter (11904): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(Failed to run model, length=3; index=3, java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
E/flutter (11904): at sq.flutter.tflite.TflitePlugin.initPoseNet(TflitePlugin.java:1262)
E/flutter (11904): at sq.flutter.tflite.TflitePlugin$RunPoseNet.<init>(TflitePlugin.java:1290)
E/flutter (11904): at sq.flutter.tflite.TflitePlugin.runPoseNetOnImage(TflitePlugin.java:1217)
E/flutter (11904): at sq.flutter.tflite.TflitePlugin.onMethodCall(TflitePlugin.java:179)
E/flutter (11904): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/flutter (11904): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:178)
E/flutter (11904): at io.flutter.embedding.engine.dart.DartMessenger.lambda$handleMessageFromDart$0$DartMessenger(DartMessenger.java:206)
E/flutter (11904): at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$6ZD1MYkhaLxyPjtoFDxe45u43DI.run(Unknown Source:12)
E/flutter (11904): at android.os.Handler.handleCallback(Handler.java:938)
E/flutter (11904): at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter (11904): at android.os.Looper.loop(Looper.java:223)
E/flutter (11904): at android.app.ActivityThread.main(ActivityThread.java:7656)
E/flutter (11904): at java.lang.reflect.Method.invoke(Native Method)
E/flutter (11904): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/flutter (11904): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter (11904): , null)
E/flutter (11904): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter (11904): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
E/flutter (11904): <asynchronous suspension>
E/flutter (11904): #2 Tflite.runPoseNetOnImage (package:tflite/tflite.dart:350:12)
E/flutter (11904): <asynchronous suspension>
E/flutter (11904): #3 _SelectImageState.poseNet (package:model_ui/main/select_image.dart:105:18)
E/flutter (11904): <asynchronous suspension>
E/flutter (11904):
any help will be appreciated!
thank you!
I'm not an expert. But tflite plugin is out of date. You should try your luck with the tflite_flutter, which is also a bit outdated. But worked for me.

Flutter Dart - Calling sqflite function in Isolate causes error

I have a sqflite database that works, unit tests work. I want to call a sqflite method in onTap method. I understand I cannot execute the sqflite function in the main UI thread (Flutter tells me that at runtime). So I created an Isolate to call the sqflite function, which gives a different error. The Isolate works if I don't call the sqflite function, works if I just return a bool. Here is the code and exception - thanks for any suggestions:
Snippet from UI
Widget loginButton(BuildContext context) {
return Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 20.0, right: 5.0, top: 20.0, bottom: 0.0),
child: GestureDetector(
onTap: () {
if (emailController.text.length > 1 && passwordController.text.length > 7) {
/// Find user. Then...
doCheckIfFoundUser(emailController.text.trim(), passwordController.text);
} else {
printUserNotFound();
}
},
child: buttonContainer(Colors.indigo, "Login", 20.0),
),
),
);
Method called from UI
doCheckIfFoundUser(String email, String password) async {
var result;
List<String> emailPasswordList = new List();
emailPasswordList.add(email);
emailPasswordList.add(password);
var receivePort = new ReceivePort();
Isolate.spawn(callbackFunction, receivePort.sendPort);
SendPort sendPort = await receivePort.first;
var ans = await sendReceive(sendPort, emailPasswordList);
setState(() {
result = ans;
print("The value is $result - please do your thing");
});
}
Isolate Callback
static void callbackFunction(SendPort callerSendPort) async {
ReceivePort newIsolateReceivePort = ReceivePort();
callerSendPort.send(newIsolateReceivePort.sendPort);
var msg = await newIsolateReceivePort.first;
List<String> emailPasswordList = msg[0];
print("email: ${emailPasswordList[0]}, password: ${emailPasswordList[1]}");
bool foundUser = await searchForUser(emailPasswordList[0], emailPasswordList[1]);
SendPort replyPort = msg[1];
replyPort.send(foundUser);
}
Future sendReceive(SendPort send, message) {
ReceivePort receivePort = ReceivePort();
send.send([message, receivePort.sendPort]);
return receivePort.first;}
I/flutter ( 2073): email: email, password: Passw0rd
E/flutter ( 2073): [ERROR:flutter/runtime/dart_isolate.cc(805)] Unhandled exception:
E/flutter ( 2073): error: native function 'Window_sendPlatformMessage' (4 arguments) cannot be found
E/flutter ( 2073): #0 Window.sendPlatformMessage (dart:ui/window.dart:1089:9)
E/flutter ( 2073): #1 _DefaultBinaryMessenger._sendPlatformMessage (package:flutter/src/services/binary_messenger.dart:85:15)
E/flutter ( 2073): #2 _DefaultBinaryMessenger.send (package:flutter/src/services/binary_messenger.dart:129:12)
E/flutter ( 2073): #3 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:309:51)
E/flutter ( 2073):
E/flutter ( 2073): #4 invokeMethod (package:sqflite/src/sqflite_impl.dart:18:34)
E/flutter ( 2073):
E/flutter ( 2073): #5 SqfliteDatabaseFactoryImpl.invokeMethod (package:sqflite/src/factory_impl.dart:33:7)
E/flutter ( 2073): #6 _SqfliteDatabaseFactoryImpl&Object&SqfliteDatabaseFactoryMixin.safeInvokeMethod. (package:sqflite/src/factory_mixin.dart:22:35)
E/flutter ( 2073): #7 wrapDatabaseException (package:sqflite/src/exception_impl.dart:7:34)
E/flutter ( 2073): .
E/flutter ( 2073): #8 SqfliteDatabaseFactoryImpl.wrapDatabaseException (package:sqflite/src/factory_impl.dart:29:7).
E/flutter ( 2073): #9 _SqfliteDatabaseFactoryImpl&Object&SqfliteDatabaseFactoryMixin.safeInvokeMethod (package:sqflite/src/factory_mixin.dart:22:7).
E/flutter ( 2073): #10 _SqfliteDatabaseFactoryImpl&Object&SqfliteDatabaseFactoryMixin.getDatabasesPath (package:sqflite/src/factory_mixin.dart:136:17).
E/flutter ( 2073):
E/flutter ( 2073): #11 getDatabasesPath. (package:sqflite/sqflite.dart:166:54)
E/flutter ( 2073): #12 UsersSqflite.init (package:himrepo/controller/users_database.dart:20:47).
E/flutter ( 2073): .
E/flutter ( 2073): #13 _LoginPageState.searchForUser. (package:himrepo/ui/login.dart:268:24).
E/flutter ( 2073): .
E/flutter ( 2073): #14 _LoginPageState.callbackFunction(package:himrepo/ui/login.dart:166:28).
E/flutter ( 2073): .
E/flutter ( 2073): #15 _startIsolate.. (dart:isolate-patch/isolate_patch.dart:304:17).
E/flutter ( 2073): #16 _RawReceivePortImpl._handleMessage(dart:isolate-patch/isolate_patch.dart:172:12).
Executing Sqflite method directly gives
E/AndroidRuntime( 6628): FATAL EXCEPTION: Sqflite
E/AndroidRuntime( 6628): java.lang.RuntimeException: Methods marked with #UiThread must be executed on the main thread. Current thread: Sqflite
E/AndroidRuntime( 6628): at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:794)
E/AndroidRuntime( 6628): at io.flutter.embedding.engine.FlutterJNI.invokePlatformMessageResponseCallback(FlutterJNI.java:727)
E/AndroidRuntime( 6628): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:140)
E/AndroidRuntime( 6628): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:225)
E/AndroidRuntime( 6628): at com.tekartik.sqflite.SqflitePlugin$6.run(SqflitePlugin.java:778)
E/AndroidRuntime( 6628): at android.os.Handler.handleCallback(Handler.java:873)
E/AndroidRuntime( 6628): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 6628): at android.os.Looper.loop(Looper.java:193)
E/AndroidRuntime( 6628): at android.os.HandlerThread.run(HandlerThread.java:65)
Thanks to #pskink for helping. The Isolate works, but I was getting exceptions when running sqflite from a Widget onTap event. It turned out that a plugin (json_annotation) I included in my pubspec.yaml was somehow contributing to the exception: "Methods marked with #UiThread must be executed on the main thread. Current thread: Sqflite". After removing that plugin the app works.