Flutter video_player with URL from Firestore Document

junsiong2008

I'm trying to play a video from a URL of a Firestore Document. To play a video in Flutter, I have to instantiate its Url in the init() method. I set a default URL to a butterfly video, and the value was supposed to be replaced by the URL obtained from Firestore. (So that it is easy for me to see if the code works). However, the code does not work properly. I got an error that says "NoSuchMethodError: The getter 'value' was called on null".

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // Create the initialization Future outside of build
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: _initialization,
        builder: (context, snapshot) {
          // Check for error
          if (snapshot.hasError) {
            print(snapshot.error);
            return Center(
              child: Container(
                child: Text(
                  "Something went wrong",
                  textDirection: TextDirection.ltr,
                ),
              ),
            );
          }

          //Once complete, show your application
          if (snapshot.connectionState == ConnectionState.done) {
            return MaterialApp(
              title: 'Flutter Demo',
              home: VideoPlayerScreen(),
            );
          }

          return CircularProgressIndicator();
        });
  }
}

class VideoPlayerScreen extends StatefulWidget {
 
  @override
  _VideoPlayerScreenState createState() => _VideoPlayerScreenState();
}

class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
  VideoPlayerController _controller;
  Future<void> _initializeVideoPlayerFuture;
  FirebaseFirestore firestore = FirebaseFirestore.instance;
  String videoUrl =
      'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4';

  @override
  void initState() {
    firestore.collection("videos").get().then((QuerySnapshot querySnapshot) => {
          querySnapshot.docs.forEach((doc) {
            // _controller.dispose();
            videoUrl = doc["videoUrl"];
            _controller = VideoPlayerController.network(videoUrl);
            _initializeVideoPlayerFuture = _controller.initialize();
            print(videoUrl);
          })
        });
    // _controller = VideoPlayerController.network(videoUrl);
    // _initializeVideoPlayerFuture = _controller.initialize();
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter Video Player"),
      ),
      body: FutureBuilder(
        future: _initializeVideoPlayerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return Column(
              children: [
                AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                ),
              ],
            );
          } else {
            return Center(child: CircularProgressIndicator());
          }
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            if (_controller.value.isPlaying) {
              _controller.pause();
            } else {
              _controller.play();
            }
          });
        },
        child: Icon(
          _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}

Peter Haddad

Try the following:

  @override
  void initState() {
    super.initState();
    firestore.collection("videos").get().then((QuerySnapshot querySnapshot) => {
          querySnapshot.docs.forEach((doc) {
            videoUrl = doc["videoUrl"];
            _controller = VideoPlayerController.network(videoUrl);
            _initializeVideoPlayerFuture = _controller.initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
         setState(() {});
            });
         });
     });
  }

Since initialize() is asynchronous, then you can use the method then which will get called when the future completes. Inside the callback, you can call setState() which will trigger a rebuild and notify the framework that the internal state of the widgets has changed .

https://pub.dev/packages/video_player

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Flutter video_player restart video from start on tap

How to get buffering status from video_player flutter plugin?

Flutter - video_player fullscreen

Flutter video_player dispose

How to play video that picked from image_picker using video_player flutter

Flutter video_player can not show the video

Flutter retrieving Document data from Firestore

Deleting document from cloud_firestore in flutter

Unable to retrieve document from Firestore with Flutter

How to get a subcollection from a document firestore flutter

Adaptive HLS Streaming - Flutter video_player

Query a single document from Firestore in Flutter (cloud_firestore Plugin)

Flutter Firestore - How to get data from a Document Reference in a Document Field?

Retrieve document from Firestore using URL

How to get specified value from firestore with `DOCUMENT ID` with Flutter

How to query using unique document id in Firestore from flutter?

Flutter - Delete a particular field from a document in cloud firestore?

how to obtain a particular document from a collection in firestore using Flutter

Get a single document from Firebase Firestore query with Flutter

how to fetch data from firestore array of a document with flutter?

How to get a specific document from QuerySnapshot in Flutter Firestore?

Updating Firestore Document Boolean value from ListTile Flutter

Flutter - video_player listener being called very slowly

The video_player plugin fails to play videos - Flutter

Flutter video_player plugin - VideoPlayerController.seekTo

Ho to catch flutter video_player plugin error?

Flutter video_player for login screen background brings black screen

Flutter video_player cannot load multiple videos in one page

Getting the documentID of a Firestore document in flutter?