SOUGAT RIYADH
SOUGAT RIYADH

Reputation: 701

Flutter taking more time to load videos from Firestore

I am building a flutter app which fetch videos from firestore storage but it takes lot of time to load all videos and when ever i open the app it always download videos. I want to build the app just like tiktok that it load only a video at a time instead of downloading the entire videos present in the firestore database.Below is the dart code.

class Timeline extends StatefulWidget {
  final User currentUser;
  Timeline({this.currentUser});
  @override
  _TimelineState createState() => _TimelineState();
}
class _TimelineState extends State<Timeline> with AutomaticKeepAliveClientMixin{
  List<Post> posts;
  buildTimeline() {
        PreloadPageView.builder(
          scrollDirection: Axis.vertical,
          itemCount: posts.length,
          preloadPagesCount: 100000,
          itemBuilder: (BuildContext context, int index) {
            return
              Stack(
                children: <Widget>[
                  //AppVideoPlayer(mediaUrl: posts[index].mediaUrl), onScreenControls(posts[index])
                  posts[index],
                ],)
  void initState() {
    super.initState();
    getTimeline();
  }
  getTimeline() async {
    QuerySnapshot snapshot = await timelineRef
        .document(widget.currentUser.id)
        .collection('timelinePosts')
        .orderBy('timestamp', descending: true)
        .getDocuments();
    List<Post> posts =
    snapshot.documents.map((doc) => Post.fromDocument(doc)).toList();
    setState(() {
      this.posts = posts;
    });
  }
  @override
  Widget build(context) {
    return Scaffold(
        backgroundColor: Colors.black,
        //appBar: header(context, isAppTitle: true),
        body: RefreshIndicator(
            onRefresh: () => getTimeline(),
            child: buildTimeline()));
  }
}

class Post extends StatefulWidget {
  final String postId;
  final String ownerId;
  final String username;
  final String mediaUrl;
  Post({
    this.postId,
    this.ownerId,
    this.username,
    this.mediaUrl,
  });
  factory Post.fromDocument(DocumentSnapshot doc) {
    return Post(
      postId: doc['postId'],
      ownerId: doc['ownerId'],
      username: doc['username'],
      mediaUrl: doc['mediaUrl'],
    );
  }
  @override
  _PostState createState() => _PostState(
        postId: this.postId,
        ownerId: this.ownerId,
        username: this.username,
        mediaUrl: this.mediaUrl,
      );
}
class _PostState extends State<Post> {
  final String currentUserId = currentUser?.id;
  final String postId;
  final String ownerId;
  final String username;
  final String mediaUrl;
  _PostState({
    this.postId,
    this.ownerId,
    this.username,
    this.mediaUrl,
  });
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Stack(
          children: <Widget>[
            AppVideoPlayer(mediaUrl: mediaUrl),
          ],
        ),
      ],
    );
  }

Upvotes: 2

Views: 895

Answers (1)

Michal Olechowski
Michal Olechowski

Reputation: 646

With this code you essentially querying all documents of user widget.currentUser.id in collection timelinePosts

    QuerySnapshot snapshot = await timelineRef
    .document(widget.currentUser.id)
    .collection('timelinePosts')
    .orderBy('timestamp', descending: true)
    .getDocuments();

Try get specific document you require like that:

var document = await Firestore.instance.collection('COLLECTION_NAME').document('TESTID1');document.get() => then(function(document) {print(document('name'));}

For improving performance you could pre download your videos and cache them before user actually need to use them.

Upvotes: 1

Related Questions