我是新的Flutter和最近开始工作,我的第一个Flutter项目。
这个想法是当用户触摸按钮/图像时,它应该播放声音。
我使用android studio来创建这个应用程序,我导入了一个名为Audioplayers的软件包来帮助播放音频。
当我运行代码时,应用程序被构建并显示图像,但当我点击图像时,它给了我一个我无法解决的错误:
Unhandled Exception: NoSuchMethodError: The getter 'position' was called on null
这是密码:
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'),
),
),
),
),
),
这是错误:
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):
我不知道为什么我得到这个错误。
1条答案
按热度按时间c3frrgcw1#
所以我设法通过导入一些新的包并删除旧的包来解决这个错误。
导入
PositionTapDetector
并将其替换为Gesturedetector
。将
Audioplayer
更改为Audio_Cache
但我认为主要的问题是
playLocal()
方法,它从设备位置而不是资产文件夹播放音频。最后的代码是