Skip to content

Not able to access Pedometer stream in foreground service #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
darshansatra1 opened this issue Jul 1, 2021 · 3 comments
Closed

Not able to access Pedometer stream in foreground service #5

darshansatra1 opened this issue Jul 1, 2021 · 3 comments

Comments

@darshansatra1
Copy link

While running the foreground service I want to access the pedometer stream. Here I am using pedometer package which exposes a stream for accessing steps of the device. When I was using flutter_foreground_service_plugin I did not face any issue accessing the stream. But using this plugin I am facing an issue related to ServicesBinding.defaultBinaryMessenger. I also placed
WidgetsFlutterBinding.ensureInitialized(); in main.dart but it is still giving me this error. It would be a great help if you can look into it cause I don't want to switch back to flutter_foreground_service_plugin since this plugin is quite awesome and well documented.

Code

Stream _stepCountStream = Pedometer.stepCountStream;
final StepCount steps = await _stepCountStream.first;

Error

E/flutter (23365): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter (23365): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter (23365): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter (23365): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:97:7)
E/flutter (23365): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:110:4)
E/flutter (23365): #2      EventChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:512:62)
E/flutter (23365): #3      EventChannel.receiveBroadcastStream.<anonymous closure> (package:flutter/src/services/platform_channel.dart:532:7)
E/flutter (23365): #4      EventChannel.receiveBroadcastStream.<anonymous closure> (package:flutter/src/services/platform_channel.dart:531:64)
E/flutter (23365): #5      _runGuarded (dart:async/stream_controller.dart:773:24)
E/flutter (23365): #6      _BroadcastStreamController._subscribe (dart:async/broadcast_stream_controller.dart:207:7)
E/flutter (23365): #7      _ControllerStream._createSubscription (dart:async/stream_controller.dart:786:19)
E/flutter (23365): #8      _StreamImpl.listen (dart:async/stream_impl.dart:473:9)
E/flutter (23365): #9      new _ForwardingStreamSubscription (dart:async/stream_pipe.dart:114:10)
E/flutter (23365): #10     _ForwardingStream._createSubscription (dart:async/stream_pipe.dart:86:16)
E/flutter (23365): #11     _ForwardingStream.listen (dart:async/stream_pipe.dart:81:12)
E/flutter (23365): #12     Stream.first (dart:async/stream.dart:1211:14)
E/flutter (23365): #13     periodicTaskFun (package:realfit/core/background/foreground_service.dart:70:50)
E/flutter (23365): #14     _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:140:25)
E/flutter (23365): #15     _rootRun (dart:async/zone.dart:1354:13)
E/flutter (23365): #16     _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (23365): #17     _runZoned (dart:async/zone.dart:1788:10)
E/flutter (23365): #18     runZonedGuarded (dart:async/zone.dart:1776:12)
E/flutter (23365): #19     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:133:5)
E/flutter (23365): #20     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
E/flutter (23365): #21     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter (23365): 

@Dev-hwang
Copy link
Owner

I would like to know at what point this problem occurs. When I tested this didn't happen.

image

image

@darshansatra1
Copy link
Author

Yess the issue is gone now. The problem was I didn't wrap my code around FlutterForegroundTask.initDispatcher. Thank you

@Dev-hwang
Copy link
Owner

@darshansatra1

You can easily subscribe to stream by upgrading the flutter_foreground_task plugin and using this method.

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants