声明已处理的异常

时间:2019-03-06 07:19:17

标签: android ios flutter

From this link I took the player code enter image description here这是我的代码:

class ChewieDemo extends StatefulWidget {

   Datum imageData;
   SubContentsDatum subContentsData;
   List<Datum>imageUrls;
   List<SubCategoryData> categoryData;
   SubCategoryData catdata;
   SharedPreferences preferences;


   ChewieDemo({this.title = 'Player',Key key, this.imageData,this.categoryData}): super(key:key);

  ChewieDemo.fromChewieDemo({this.title = 'Player',Key key,this.subContentsData}):super(key:key);

 // ChewieDemo.fromChewieDemo({ChewieDemo demo,this.title = 'Player',Key key,@required demo.categoryData}):super(key:key);
   String title;

  @override
  State<StatefulWidget> createState() {
    return _ChewieDemoState();
  }
}

class _ChewieDemoState extends State<ChewieDemo> {

  TargetPlatform _platform;
  VideoPlayerController _videoPlayerController1;
  VideoPlayerController _videoPlayerController2;
  ChewieController _chewieController;
  List<Tab>categoryNames = [];
  List<String>subCategories = [];
  Tab tab;
  List<SubContentsDatum>images = [];
  String dataUrl;
  SharedPreferences preferences;


  void initPreferences() async {

  preferences = await SharedPreferences.getInstance();
   /* dataUrl = preferences.getString("dataurl");
    print('dataurl:$dataUrl');
*/
  }

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

    //initPreferences();

    if (widget.categoryData != null) {
      print('data: $categoryNames');
      categoryNames.clear();
      for (int i = 0; i < widget.categoryData.length; i++) {
        tab = Tab(text: widget.categoryData[i].name);
        categoryNames.add(tab);
      }
      // print('data: $categoryNames');
    } else {

    }
    // 'https://www.sample-videos.com/video123/mp4/480/big_buck_bunny_480p_20mb.mp4'

    initPlayer();
  }

  @override
  void dispose() {
    _videoPlayerController1.dispose();
    //  _videoPlayerController2.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: widget.title,
      theme: ThemeData.light().copyWith(
        platform: _platform ?? Theme
            .of(context)
            .platform,
      ),
      home: DefaultTabController(
        length: categoryNames.length,
        child: Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
            leading: IconButton(icon:
            Icon(Icons.arrow_back),
                onPressed: (){
                  Navigator.of(context).pushNamed('/screen1');
                }),
          ),

          body: SafeArea(
              child: Column(
                children: <Widget>[
                  Chewie(
                    controller: _chewieController,
                  ),
                  TabBar(
                    labelColor: Colors.black,
                    tabs: categoryNames,
                  ),
                  Expanded(
                    child: TabBarView(
                      children: List<Widget>.generate(
                          categoryNames.length, (int index) {
                        print('catid:${widget.categoryData[index].id}');
                        int categoryId = widget.categoryData[index].id;
                        return ImageList(categoryId: categoryId);
                      }),
                    ),
                  )
                ],
              )
          ),
        ),
      ),
    );
  }

  void initPlayer() async {
   // initPreferences();

    preferences = await SharedPreferences.getInstance();

    dataUrl = preferences.getString("dataurl");
   // dataUrl = 'http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_314t2peg/format/url/protocol/http';

    print('dataurl:$dataUrl');

    //widget.imageData.dataUrl = "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_314t2peg/format/url/protocol/http";
    //print('url player :${widget.imageData.dataUrl}');
    _videoPlayerController1 = VideoPlayerController.network('$dataUrl');
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController1,
      aspectRatio: 3 / 2,
      autoPlay: true,
      looping: true,
    );
  }


}

嗨,

我试图从首选项中获取一个字符串并传递给播放器。但我遇到类似"Failed assertion"

的错误
  

错误:断言失败:'controller!= null'您必须提供一个咀嚼者   控制器。

我不知道我为initPlayer()给出的 async 是否有问题。请检查我的代码并通知我。

0 个答案:

没有答案