I'm new to flutter and have recently started working on my first flutter project.
The idea is that when the user touches the button / image it should play a sound.
I'm using android studio to create the app. I have imported a package called Audioplayers to help play the audio.
When I run the code, the app is built and it displays the image, but when I tap on the image, it gives me an error that I'm unable to resolve:
Unhandled Exception: NoSuchMethodError: The getter 'position' was called on null
This is the code:
MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Center(
child: GestureDetector(
onTap: () {
AudioPlayer audioPlayer = AudioPlayer();
audioPlayer.setVolume(1.0);
playLocal() async {
int result = await audioPlayer.play(
'path/to/mp3',
isLocal: true);
}
},
child: Image(
image: AssetImage('assetsproj/images/PngItem_4931119.png'),
),
),
),
),
),
This is the error:
E/flutter (11532): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'position' was called on null.
E/flutter (11532): Receiver: null
E/flutter (11532): Tried calling: position
E/flutter (11532): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (11532): #1 TapGestureRecognizer.handleTapDown (package:flutter/src/gestures/tap.dart:456:28)
E/flutter (11532): #2 BaseTapGestureRecognizer._checkDown (package:flutter/src/gestures/tap.dart:256:5)
E/flutter (11532): #3 BaseTapGestureRecognizer.didExceedDeadline (package:flutter/src/gestures/tap.dart:227:5)
E/flutter (11532): #4 PrimaryPointerGestureRecognizer.didExceedDeadlineWithEvent (package:flutter/src/gestures/recognizer.dart:493:5)
E/flutter (11532): #5 PrimaryPointerGestureRecognizer.addAllowedPointer.<anonymous closure> (package:flutter/src/gestures/recognizer.dart:446:40)
E/flutter (11532): #6 _rootRun (dart:async/zone.dart:1122:38)
E/flutter (11532): #7 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (11532): #8 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter (11532): #9 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
E/flutter (11532): #10 _rootRun (dart:async/zone.dart:1126:13)
E/flutter (11532): #11 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (11532): #12 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949:23)
E/flutter (11532): #13 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
E/flutter (11532): #14 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter (11532): #15 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter (11532): #16 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter (11532):
I'm not sure why I get this error.
So I've managed to solve the error by importing a few new packages and removing the old package.
Imported PositionTapDetector
and replaced Gesturedetector
with it.
Changed Audioplayer
to Audio_Cache
But I think the main problem was the playLocal()
method which plays audio from the devices location and not the asset folder.
So the final code is
child: PositionedTapDetector(
onTap: (center) {
AudioCache player = AudioCache();
player.play('audio/movie_1.mp3');
},
child: Image(
image: AssetImage('assets/images/PngItem_4931119.png'),
),