未处理的异常:使用 audioplayers 包查找停用的小部件的祖先是不安全的错误

问题描述

我的应用程序中出现这种错误。我正在使用 audioplayers 包来播放简短的声音。但是在玩了大约 5 次后,我收到了一个错误,即: 此时小部件的元素树的状态不再稳定。 要在其 dispose() 方法中安全地引用小部件的祖先,请通过在小部件的 didChangeDependencies() 方法调用 DependOnInheritedWidgetofExactType() 来保存对祖先的引用。

这是我的代码

class NewOrders extends StatefulWidget {
  @override
  _NewOrdeRSState createState() => _NewOrdeRSState();
}

class _NewOrdeRSState extends State<NewOrders> {
  final SocketService socketService = injector.get<SocketService>();
  final _orderRepo = OrderRepo(type: 'Accepted');

  int activeItem = 0;
  AudioPlayer audioPlayer = AudioPlayer();
  final player = AudioCache(prefix: 'assets/sounds/');

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<OrderBloc>(
          create: (context) =>
              OrderBloc(orderRepo: _orderRepo)..add(OrderLoadEvent()),),BlocProvider<OrderInfoBloc>(
          create: (context) => OrderInfoBloc(),)
      ],child: Scaffold(
        body: BlocBuilder<OrderBloc,OrderState>(
          builder: (BuildContext context,state) {
            if (state is OrderLoadingState) {
              return Center(child: Customloading());
            }
            if (state is OrderLoadedState) {
              if (state.loadedOrders.length > 0) {
                OrderInfoRepo _orderInfoRepo =
                    OrderInfoRepo(tableId: state.loadedOrders[activeItem].id);
                context
                    .read<OrderInfoBloc>()
                    .add(OrderInfoLoadEvent(orderInfoRepo: _orderInfoRepo));

                socketService.socket.on(
                  'createOrder',(data) => {
                    context.read<OrderBloc>().add(OrderLoadEvent()),player.play('accomplished.mp3'),Future.delayed(const Duration(milliseconds: 500),() => player.clear('accomplished.mp3'))
                  },);
                return Container(
                  child: Row(
                    children: [
                      Container(
                        decoration: Boxdecoration(
                          color: tertiaryColor,BoxShadow: [
                            BoxShadow(
                                color: Colors.grey,offset: Offset.zero,blurRadius: 1,spreadRadius: 0.5)
                          ],child: ListView.builder(
                          itemBuilder: (context,int index) {
                            if (index == activeItem) {
                              return Container(
                                height: 50,color: Colors.white,child: TextButton(
                                  onpressed: () {},child: Container(
                                    child: Text(
                                      '${state.loadedOrders[index].tableNum} - стол',style: TextStyle(
                                          fontSize: 20,color: tertiaryColor),);
                            } else {
                              return Container(
                                height: 50,// color: tertiaryColor,child: TextButton(
                                  onpressed: () {
                                    setState(() {
                                      activeItem = index;
                                    });
                                    context.read<OrderInfoBloc>().add(
                                        OrderInfoLoadEvent(
                                            orderInfoRepo: _orderInfoRepo));
                                  },color: Colors.white),);
                            }
                          },itemCount: state.loadedOrders.length,width: 250,Expanded(
                        child: OrderInfoWidget(),)
                    ],);
              } else {
                return EmptyCart();
              }
            } else {
              return NetworkErrorWidget();
            }
          },);
  }
}

出现上述错误的原因是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...