如何依次遍历Flutter中Map的对象值,并在迭代时逐一返回每个值?

时间:2018-10-18 21:04:21

标签: flutter

  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;
    }
  }

我正在尝试迭代并返回Map /的每个值以按顺序访问每个值。但是,当我尝试循环并返回值时,我仅获得对象的第一个值。我该如何做才能使其遍历值数据,但又不会将数据更改为字符串类型。也就是说,每个对象的值都保持不变,而无需更改其原始组成。那就是我不想打印它,我只想要一种机制,该机制逐个顺序地访问值,并在迭代时逐个返回每个值。

1 个答案:

答案 0 :(得分:0)

以下是使用video player plugin的演示。根据雷米(Remi)在上述问题的注释中的建议,而不是Map,我创建了具有标题和控制器属性的基本视频模型,并将其添加到List中。如果您要制作类似YouTube的供稿(最有可能使用ListListView,则使用CustomScrollView(具有适当的模型)要容易得多。确保您的pubspec.yaml已使用您的视频资产进行更新(并更改了以下文件名),并且该示例对您适用。视频初始化时和播放时长时,下面的听众将打印。

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;
}