I am trying to use the audio_service package to allow background controls for my podcast playing app. The service does not start. When I call AudioService.start(), nothing is ever returned (I'm awaiting this function and trying to print a statement after this returns, but it never returns. Also, when I first attempt to play an item, it will correctly evaluate AudioService.running to false and then try to start the AudioService (hanging on the start() function). When I hit play a second time, it will evaluate AudioService.running as true (even though the start function never returned) and will then throw this error:
E/flutter ( 8726): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: PlatformException(Attempt to invoke virtual method 'void io.flutter.plugin.common.MethodChannel.invokeMethod(java.lang.String, java.lang.Object, io.flutter.plugin.common.MethodChannel$Result)' on a null object reference, null, null)
E/flutter ( 8726): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:572:7)
E/flutter ( 8726): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:161:18)
E/flutter ( 8726): <asynchronous suspension>
E/flutter ( 8726): #2 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:334:12)
E/flutter ( 8726): #3 AudioService.play (package:audio_service/audio_service.dart:895:20)
Here is my code which calls the start function:
if(AudioService.running) {
print('playing audio service: ${AudioService.running}');
AudioService.play();
} else {
MediaItem mediaItem = MediaItem(
id: newPostPod.audioUrl,
title: newPostPod.titleTextString(),
artist: newPostPod.subtitleTextString(),
);
print('starting audio service');
bool started = await AudioService.start(
backgroundTaskEntrypoint: _backgroundTaskEntrypoint,
params: {'mediaItem': mediaItem.toJson()},
);
print('audio service started');
if(AudioService.running) {
print('starting to play via media service');
AudioService.playMediaItem(mediaItem);
} else {
print('Audio Service not yet started: $started');
}
}
The code for the background task entrypoint (which is outside of any other class, as it wasn't working inside a class I had read in another issue that it had to be top-level):
_backgroundTaskEntrypoint() {
AudioServiceBackground.run(() => PlayerTask());
}
Lastly, it doesn't appear any of the code within my onStart in my BackgroundAudioTask is running (as print statements at the top of my onStart are not being run). So I'm not sure what's happening here. It isn't give me a clear error or really any hint of where to start debugging audio_service.
Calling AudioService.connect(); before starting the service will solve the error.
For me AudioService.start() would freeze until I added the WAKE_LOCK and FOREGROUND_SERVICE permissions to the AndroidManifest.xml file per the audio_service README file.
Related
I am using this "google_maps_webservice^0.0.20-nullsafety.5" in flutter
Can some one please let me know, what I am doing wrong here
Signature:
Future getDetailsByPlaceId(
String placeId, {
String? sessionToken,
List fields = const [],
String? language,
String? region, }
)
Here is how I am using it
PlacesDetailsResponse detail =
await _places.getDetailsByPlaceId(
p.placeId.toString(),
fields: ['name'], // Commenting out this line, would make it work, but i need this attribute/property to save cost
language: 'fr',
region:"fr",
);
Issue:
fields: ['name'] => this "fields" attribute keeps failing,
I have seen so many tutorials , all are suggesting the same way,
but still it fails with the below message
E/flutter ( 9854): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'String' in type cast
E/flutter ( 9854): #0 _$PlaceDetailsFromJson (package:google_maps_webservice/src/places.g.dart:138:31)
E/flutter ( 9854): #1 new PlaceDetails.fromJson (package:google_maps_webservice/src/places.dart:710:7)
E/flutter ( 9854): #2 _$PlacesDetailsResponseFromJson (package:google_maps_webservice/src/places.g.dart:294:26)
E/flutter ( 9854): #3 new PlacesDetailsResponse.fromJson (package:google_maps_webservice/src/places.dart:838:7)
E/flutter ( 9854): #4 GoogleMapsPlaces._decodeDetailsResponse (package:google_maps_webservice/src/places.dart:528:29)
E/flutter ( 9854): #5 GoogleMapsPlaces.getDetailsByPlaceId (package:google_maps_webservice/src/places.dart:126:12)
E/flutter ( 9854):
E/flutter ( 9854): #6 _LocationPickerInitialState.goToPlace (package:myproj/screens/maps/initialOwnerLocationpicker.dart:149:11)
E/flutter ( 9854):
Here is how I am using it
PlacesDetailsResponse detail =
await _places.getDetailsByPlaceId(
p.placeId.toString(),
fields: ['name'], // Commenting out this line, would make it work, but i need this attribute/property to save cost
language: 'fr',
region:"fr",
);
For the sake of the argument let's say I have an implementation of BaseAudioHandler in my application:
class AudioPlayerHandler extends BaseAudioHandler {
#override
Future<void> play() async {
throw Error();
}
}
Then when I call this function from an event handler:
void onPlayButtonTap() {
try {
audioPlayerHandler.play();
} catch (error) {
// Error is not caught, but in the console I see unhandled error message.
print(error.toString())
}
}
It doesn't catch the error thrown within the method.
Of course the example this way doesn't make sense, but I've run into this problem when trying to load an invalid url which i could not handle in 'regular' way.
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Instance of 'Error'
#0 AudioPlayerHandler.play (package:audio_service_example/main.dart:197:5)
#1 MainScreen._button.<anonymous closure> (package:audio_service_example/main.dart:145:22)
#2 _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1072:21)
#3 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:253:24)
#4 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:627:11)
#5 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:306:5)
#6 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:239:7)
#7 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:615:9)
#8 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:98:12)
#9 PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_ro<…>
I expected to be able to use a try-catch block to handle the error.
I can handle the error inside the audiohandler implementation and let's say I can propagate a custom event that tells the user of the handler that an error occured, but that solution seems to be a bit clumsy.
I'm also not sure if this is a dart programming language quirk unknown to me or an implementation problem? Tried to step through with debugger but did not find anything meaningful.
Could you please help me how this error handling should work?
Thanks you in advance!
This concerns the Dart language.
Because you defined play as an async method, play doesn't actually throw an error. It returns a future which evaluates to an error. If you await that future, you'll be able to catch the error:
try {
await audioPlayerHandler.play();
This is the code I am using.
I have checked it with different file format but still showing the same error, i have checked the "img.path" its showing the path to the image. I don't understand why I am getting an error.
String targetPath;
var result = await FlutterImageCompress.compressAndGetFile(
img.path,
targetPath,
quality: 88,
format: CompressFormat.webp,
);
Error I am getting:
[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: The method 'toLowerCase' was called on null.
Receiver: null
Tried calling: toLowerCase()
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 Validator.checkFileNameAndFormat
package:flutter_image_compress/src/validator.dart:19
#2 FlutterImageCompress.compressAndGetFile
package:flutter_image_compress/flutter_image_compress.dart:150
#3 _AddImageState.uploadFile
package:softclaw/…/addpost/addimage.dart:225
#4 _AddImageState.build.<anonymous closure>
package:softclaw/…/addpost/addimage.dart:97
<asynchronous suspension>
targetPath is the one that goes through the checkFileNameAndFormat > toLowerCase, therefore check to ensure this is not null.
I'm using socket_io_client v0.9.12 in my flutter app (still not using null safety). I am creating a socket connection with my back-end server when the app starts and allow child widgets to access it using a provider.
I am trying to start creating the connection in the constructor so that when I need to use the socket client, it would probably be initialized already.
This is my provider class which has the creation of the socket connection:
class SocketClient {
SocketClient() {
_initializer = initialize();
}
Future _initializer;
/// Socket client
Socket client;
/// Wait until the client is properly connected with the server
Future finishInitializing() => _initializer;
/// Initialize the socket connection with the server
Future initialize() async {
final completer = Completer<Socket>();
final socket = io(
'http://localhost:3000/gateway',
OptionBuilder() //
.setTransports(['websocket'])
.disableAutoConnect()
.setQuery({'token': 'TOKEN'})
.build(),
)..connect();
socket
..onConnect((_) {
completer.complete(socket);
});
client = await completer.future;
}
}
The main app widget I have:
#override
Widget build(BuildContext context) {
return Provider<SocketClient>(
create: (_) => SocketClient(),
builder: (context, _) {
return Scaffold(
...
);
},
);
}
This is how I am trying to use the socket client:
Future<void> foo(SocketClient socketClient) async {
await socketClient.finishInitializing();
final socket = socketClient.client;
socket.on(eventName, (dynamic uploadDrawingEvent) {
...
});
}
Everything works fine up to now. 🎉
However, when I try to run my tests that depend on this socket client, I see the below error:
Pending timers:
Timer (duration: 0:00:20.000000, periodic: false), created:
#0 new FakeTimer._ (package:fake_async/fake_async.dart:284:41)
#1 FakeAsync._createTimer (package:fake_async/fake_async.dart:248:27)
#2 FakeAsync.run.<anonymous closure> (package:fake_async/fake_async.dart:181:19)
#5 Manager.connect (package:socket_io_client/src/manager.dart:232:19)
#6 Manager.open (package:socket_io_client/src/manager.dart:194:41)
#7 Socket.connect (package:socket_io_client/src/socket.dart:104:8)
#8 SocketClient.initialize (package:flutter_app/web/services/socket_provider.dart:48:8)
<asynchronous suspension>
(elided 2 frames from dart:async)
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
A Timer is still pending even after the widget tree was disposed.
'package:flutter_test/src/binding.dart':
Failed assertion: line 1241 pos 12: '!timersPending'
When the exception was thrown, this was the stack:
#2 AutomatedTestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:1241:12)
#3 TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:803:7)
<asynchronous suspension>
(elided 2 frames from class _AssertionError)
...
initialize function keeps hanging after client = await completer.future; line. That future is not resolved even after the test is finished (see the error).
I would like to get ideas on how I can mock this socket client properly to avoid this error.
I'm looking more towards an answer which mocks the functionality of the socket client because I have some more tests to write to cover the event handlers.
Thank you in advance! 🙏
You can use await IOOverrides for override standard Socket method like socketConnect
Exemple:
await IOOverrides.runZoned(() async {
your code
}, socketConnect: (host, port, {sourceAddress, int sourcePort = 0, timeout}) => Future.value(mockSocket));
I am getting the following error Unhandled Exception: Null check operator and I am not sure if it is something wrong in my code or the library I am using. I am trying to test the livequery of the parse_server_sdk used by back4app because I require sending and receiving images in Realtime. Here is the relevant code:
import 'dart:io';
import 'package:filepicker_windows/filepicker_windows.dart';
import 'package:flutter/material.dart';
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart';
void main() async {
final keyApplicationId = 'EPARW6nRAAyp5uehoDE7rBEby4wtehcZf9EayykS';
final keyClientKey = 'fDaL2DjyC9YdwCwZ4RB5c5vhACROaMOO1EjjL4Zn';
final keyParseServerUrl = 'https://parseapi.back4app.com';
final LIVE_QUERY_URL = 'wss://samuraichat.b4a.io';
await Parse().initialize(keyApplicationId, keyParseServerUrl,
clientKey: keyClientKey,
autoSendSessionId: true,
liveQueryUrl: LIVE_QUERY_URL,
coreStore: CoreStoreMemoryImp());
final LiveQuery liveQuery = LiveQuery();
QueryBuilder<ParseObject> query =
QueryBuilder<ParseObject>(ParseObject('FirstClass'))
..whereEqualTo('chatId', 1);
Subscription subscription = await liveQuery.client.subscribe(query);
subscription.on(LiveQueryEvent.create, (value) {
print('*** CREATE ***: ${DateTime.now().toString()}\n $value ');
print((value as ParseObject).objectId);
print((value as ParseObject).get('message'));
});
runApp(MyApp());
}
Here is the error stack:
[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
#0 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
#2 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
#3 MethodChannelConnectivity.checkConnectivity (package:connectivity_platform_interface/src/method_channel_connectivity.dart:41:29)
#4 Connectivity.checkConnectivity (package:connectivity/connectivity.dart:46:22)
#5 Parse.checkConnectivity (package:parse_server_sdk_flutter/parse_server_sdk.dart:106:34)
#6 new LiveQueryReconnectingController (package:parse_server_sdk/src/network/parse_live_query.dart:45:28)
#7 new LiveQueryClient._internal (package:parse_server_sdk/src/network/parse_live_query.dart:146:30)
#8 LiveQueryClient._getInstance (package:parse_server_sdk/src/network/parse_live_query.dart:153:35)
#9 new LiveQuery (package:parse_server_sdk/src/network/parse_live_query.dart:416:30)
#10 main (package:chat_app/main.dart:18:37)
<asynchronous suspension>
Thanks.
Try to clean your app cache:
flutter clean
This bug should be fixed with this PR.
You use the current nullsafety branch by overriding the dependency in your pubspec.yaml:
dependency_overrides:
parse_server_sdk_flutter:
git:
url: https://github.com/parse-community/Parse-SDK-Flutter.git
ref: nullsafety
path: packages/flutter
parse_server_sdk:
git:
url: https://github.com/parse-community/Parse-SDK-Flutter.git
ref: nullsafety
path: packages/dart