Flutter中Dart级联操作符'..'在initState内无法使用的问题
Dart级联操作符
..报错"expected an identifier"的解决方法 你的代码错误出在赋值语句末尾的分号上:_videoPlayerController = VideoPlayerController.asset(asset);这行的分号切断了级联操作的链式关系,导致后续的..找不到对应的操作对象,从而抛出"expected an identifier"错误。
Dart的级联操作符..需要直接衔接在被操作的实例后面,不能被分号打断。修正后的代码如下:
void initState() { super.initState(); // select where the video is coming from asset/file/network _videoPlayerController = VideoPlayerController.asset(asset) ..setListener(() => setState(() {})) ..setLooping(true) ..initialize(); }
原理说明:去掉赋值行末尾的分号后,VideoPlayerController.asset(asset)创建的实例会直接被后续的..链式调用操作,所有的setListener、setLooping、initialize方法都是针对同一个控制器实例执行,同时最终的实例会被赋值给_videoPlayerController变量。
内容的提问来源于stack exchange,提问作者user17281101
相关产品推荐
相关产品推荐

