0
  Map<String, VideoPlayerController> controllers = {
    'one${1 + 1}': VideoPlayerController.asset('assets/videos/6.mp4'),
    'one${1 + 2}': VideoPlayerController.asset('assets/videos/2.mp4'),
    'one${1 + 3}': VideoPlayerController.asset('assets/videos/3.mp4'),
    'one${1 + 4}': VideoPlayerController.asset('assets/videos/4.mp4'),
    'one${1 + 6}': VideoPlayerController.asset('assets/videos/1.mp4'),
    'one${1 + 7}': VideoPlayerController.asset('assets/videos/7.mp4'),
    'one${1 + 8}': VideoPlayerController.asset('assets/videos/8.mp4'),
  };

  controllerLooper3() {
    for (value in controllers.values) {
      return value;
    }
  }

I am trying to iterate and return each value of the Map/to access each value sequentially. However, when I try to loop and return the value I am only getting the first value of the object. How do I do it so that it loops through the the value data but without changing that data into a string type.That is each objects value is maintained without changing its raw make up. That is I do not want to print it I just want a mechanism that is accessing the values sequentially one by one and returning each value one by one as it iterates.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
K.chim
  • 868
  • 2
  • 14
  • 25

1 Answers1

0

The below is a demo using the video player plugin. Instead of a Map, per Remi's recommendation in the comments to your question above, I created a basic video model with title and controller properties and added them to a List. It's much easier to use a List (with proper models) if you're looking to make a YouTube-like feed (most likely using a ListView or CustomScrollView). Ensure your pubspec.yaml is updated with your video assets (and change the below filenames), and the example should work for you. The listeners below will print when the video is initialized and the duration.

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Video Player Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Video Player Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<VideoModel> _controllers = [];

  @override
  void initState() {
    super.initState();

    _controllers = [
      VideoModel(
          title: 'Alpha',
          controller: VideoPlayerController.asset('assets/videos/small.mp4')),
      VideoModel(
          title: 'Beta',
          controller: VideoPlayerController.asset('assets/videos/small.mp4')),
      VideoModel(
          title: 'Gamma',
          controller: VideoPlayerController.asset('assets/videos/small.mp4')),
    ];
    _controllerLooper();
  }

  _controllerLooper() {
    for (VideoModel video in _controllers) {
      final listener = () {
        if (video.controller.value.initialized) {
          print('${video.title} - video initialized');
        }
        print('${video.title} duration: ${video.controller.value.position}');
      };
      video.controller
        ..addListener(listener)
        ..setVolume(1.0)
        ..setLooping(true)
        ..initialize();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
          itemCount: _controllers.length,
          itemBuilder: (context, index) {
            final controller = _controllers[index].controller;
            return GestureDetector(
              onTap: () {
                if (controller.value.isPlaying) {
                  controller.pause();
                } else if (!controller.value.isPlaying) {
                  controller.play();
                }
              },
              child: AspectRatio(
                aspectRatio: 16.0 / 9.0,
                child: VideoPlayer(_controllers[index].controller),
              ),
            );
          }),
    );
  }
}

class VideoModel {
  VideoModel({this.title, this.controller});

  final String title;
  final VideoPlayerController controller;
}
Albert Lardizabal
  • 6,548
  • 7
  • 34
  • 34
  • haven't yet tried your code doing some assignment but I am trying to make a news feed of videos. – K.chim Oct 19 '18 at 15:34
  • as in I want to use controllerLooper3() as an iterator then tie it to event listener that plays and pauses different videos displayed as a feed. You know like YouTube... I am really stuck.... – K.chim Oct 19 '18 at 15:37
  • I am putting your 'for loop' in controllerLooper3 method so that I tack on addListener method to listen for play and pause events. However the addListener method is throwing an error that 'addListener' was called on null. Receiver: null. I have tried your code what can I do? – K.chim Oct 19 '18 at 20:46
  • @K.chim I changed my answer after looking at the `video_player` plugin, which I think you're using. Hopefully the above helps. – Albert Lardizabal Oct 19 '18 at 23:16
  • Works like Charm!! you are Genius!! Now how do I control the play and pause? Everything is playing simultaneously at once – K.chim Oct 21 '18 at 16:22
  • @K.chim I updated the code to add a `GestureDetector` to each video cell that will play/pause the video depending on the current state. – Albert Lardizabal Oct 21 '18 at 19:19
  • your wonderful. Appreciate it. Should I have any more problems as I develop the App I'll get back to you through this post. Cheers – K.chim Nov 02 '18 at 14:23
  • wanted to know how would we implement thumbnails on the videos? you know like an image cover from a frame of the videos and displayed as an image then when you tap it starts playing the video. Like YouTube does. I tried using the thumbnails plugin but the documentation is ill explained. – K.chim Feb 14 '19 at 18:48