2

I have a String type key assigned as variable for Youtube video player. The later requires String Type initialVideoId and when I try to use my variable as initialVideoId, it keeps saying type 'int' is not a subtype of 'String'.

I double checked my variable and it is String. I even printed my variable. Then I copied my variable value(which is the String key from Api) and when i use it as initialVideoId, it runs without any issue. But when I tried to assign my variable as initialVideoId, the same error happens again. The value of my variable is changing according to the Movie selected so I cannot use a fix String key.

import 'package:flutter/material.dart';
import 'package:the_movie_booking/authentication/data/models/the_movie_api_model.dart';
import 'package:the_movie_booking/authentication/data/models/the_movie_api_model_impl.dart';
import 'package:the_movie_booking/resources/colors.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';

class YouTubeVideoPlayerMovieDetails extends StatefulWidget {
  const YouTubeVideoPlayerMovieDetails(
      {Key? key, required this.trailerVideoKey, required this.movieID})
      : super(key: key);
  final String? trailerVideoKey;
  final int? movieID;

  @override
  State<YouTubeVideoPlayerMovieDetails> createState() =>
      _YouTubeVideoPlayerMovieDetailsState();
}

class _YouTubeVideoPlayerMovieDetailsState
    extends State<YouTubeVideoPlayerMovieDetails> {
  late YoutubePlayerController _controller;
  String? videoKey;
  final TheMovieApiModel _movieApiModel = TheMovieApiModelImpl();

  @override
  void initState() {
    /// Get Trailer Video from network
    _movieApiModel.getMovieTrailers(widget.movieID ?? 0).then((response) {
      setState(() {
        videoKey = response.results?.last.key ?? "";
      });
      print("======================================> VIDEO KEY $videoKey"); // videoKey to use as initialVideoId || print value ===> 0wiBHEACNHs
    }).catchError((error) {
      debugPrint(error);
    });

    _controller = YoutubePlayerController(
      initialVideoId: videoKey ?? "0wiBHEACNHs", // I assigned videoKey here, but it doesn't work || but if i use the print value (0wiBHEACNHs), the video player runs smoothly.
      flags: const YoutubePlayerFlags(autoPlay: true, loop: true),
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return YoutubePlayer(
      controller: _controller,
      showVideoProgressIndicator: true,
      progressIndicatorColor: APP_COLOR_SECONDARY_COLOR,
    );
  }
}

0 Answers0