Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When and where should I add my own controller? #5

Closed
tagerjump opened this issue Apr 19, 2019 · 1 comment
Closed

When and where should I add my own controller? #5

tagerjump opened this issue Apr 19, 2019 · 1 comment

Comments

@tagerjump
Copy link

Very good package but I'm a kind of confused about how to add my own player controller.

I declear YoutubePlayer as final inside of parent widget's build() and set hideControls to true. But I don't know when and where to init my own controller. Now my code is something like

final YoutubePlayer youtubePlayer = YoutubePlayer(
...
onPlayerInitialized: (controller) {
_player = PlayerPage(controller, true);
setState(() {
_stackList.add(_player);
});
},
};

_stackList.add(youtubePlayer);

return Stack(
children: _stackList,
);

However, I get error for using setState() inside onPlayerInitialized().

I/flutter (13939): setState() or markNeedsBuild() called during build.
I/flutter (13939): This VideoPage widget cannot be marked as needing to build because the framework is already in the
I/flutter (13939): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter (13939): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter (13939): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter (13939): Otherwise, the framework might not visit this widget during this build phase.
I/flutter (13939): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (13939): VideoPage(state: _VideoPageState#af93c)
I/flutter (13939): The widget which was currently being built when the offending call was made was:
I/flutter (13939): NotificationListener
I/flutter (13939):
I/flutter (13939): When the exception was thrown, this was the stack:
...

Seems the onPlayerInitialized run before parent build done. But I can't refer to _controller either inside the parent build() because it's null at that time.

Could you tell me how to do that correctly? Or even better if you've got a example about adding a custom controller. Thank you.

@tagerjump
Copy link
Author

Solved... Just need another build function like
Widget _buildPlayer(YoutubePlayerController controller) {
return PlayerPage(controller, true);
}
to build the controller. But I don't know why.

@vinodlee vinodlee mentioned this issue Aug 25, 2019
ynhi0711 pushed a commit to ynhi0711/youtube_player_flutter that referenced this issue Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant