1

I need to initialize the video player but the url is coming from an API call.

What i tried so far:

  late VideoPlayerController _controller;

  Future<Tweet> postsFuture = getPosts();

  static Future<Tweet> getPosts() async {
    ...
    return tweet;
  }

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

void _initializeVideoController(String videoUrl) {
    _controller = VideoPlayerController.networkUrl(Uri.parse(videoUrl))
      ..initialize().then((_) {
        setState(() {});
      });
  }

My Widget build is like this:

return Scaffold(
      body: Center(
        child: FutureBuilder<Tweet>(
          future: postsFuture,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final posts = snapshot.data!; 
              String? videoUrl = posts.video;

              if (videoUrl != null) {
                _initializeVideoController(videoUrl);

                return Center(
                  child: _controller.value.isInitialized
                      ? AspectRatio(
                          aspectRatio: _controller.value.aspectRatio,
                          child: VideoPlayer(_controller),
                        )
                      : Container(),
                );
              } ...
          },
        ),
      ),

but when i run this, i have this Exception:

LateError (LateInitializationError: Field '_controller@28272344' has not been initialized.)

What am I doing wrong?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.