【问题标题】:flutter [Only static members can be accessed in initializers]flutter [在初始化程序中只能访问静态成员]
【发布时间】:2020-05-09 17:47:17
【问题描述】:

我是颤振和飞镖的真正初学者。 我在使用 [ youtube_player_flutter: ^6.1.1] 播放 youtube 视频时遇到问题 我创建了一个带有 youtube 链接的 Json 文件,我想将它与 [youtube_player_flutter: ^6.1.1] 链接。但它总是显示错误消息[在初始化程序中只能访问静态成员]

          @override
      Widget build(BuildContext context) {
        // this function is called before the build so that
        // the string assettoload is avialable to the DefaultAssetBuilder
        setasset();
        // and now we return the FutureBuilder to load and decode JSON
        return FutureBuilder(
          future:
              DefaultAssetBundle.of(context).loadString(assettoload, cache: true),
          builder: (context, snapshot) {
            List mydata = json.decode(snapshot.data.toString());
            if (mydata == null) {
              return Scaffold(
                body: Center(
                  child: Text(
                    "Loading",
                  ),
                ),
              );
            } else {
              return quizpage(mydata: mydata);
            }
          },
        );
      }
    }

    class quizpage extends StatefulWidget {
      final dynamic mydata;
      ////////var youtubeUrl;
      quizpage({Key key, @required this.mydata}) : super(key: key);
      @override
      _quizpageState createState() => _quizpageState(mydata);
    }

    class _quizpageState extends State<quizpage> {
      var mydata;

      _quizpageState(this.mydata);

      int marks = 0;
      int i = 1;

  @override
  void setState(fn) {
    if (mounted) {
      super.setState(fn);
    }
  }
  YoutubePlayerController _controller;

  @override
  void initState() {
    _controller = YoutubePlayerController(
        initialVideoId: YoutubePlayer.convertUrlToId(mydata[4]["1"]));
    super.initState();
  }

  void nextquestion() {
    setState(() {
      if (i < 10) {
        i++;
      } else {
        Navigator.of(context).pushReplacement(MaterialPageRoute(
          builder: (context) => resultpage(marks: marks),
        ));
      }

问题是我想让 [String videoURL ] 播放我的 json 数据文件中的视频列表。

提前致谢。

【问题讨论】:

  • 您两次声明了变量mydata。第一个实例在状态对象中,第二个实例在有状态小部件@Montasser Mohamed
  • 你好,我这里没有看到任何错误,还有其他代码我们可以看看吗?
  • 感谢您的关注。我更新了代码,如果你能帮我一把。

标签: flutter dart


【解决方案1】:

可能是您对变量mydata 进行了两次编码。这是您应该遵循的格式。为了在构造函数中使用 StatefulWidget 中的变量,请使用 widget.mydata。不必声明两次。

代码:

class Quizpage extends StatefulWidget {
  final dynamic mydata;

  quizpage({Key key, @required this.mydata}) : super(key: key);

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

class _QuizpageState extends State<Quizpage> {
  /* 
    You can make use of your mydata in this class like this:
    widget.mydata, and you will be able to make it work
  */
  Color colortoshow = Colors.indigoAccent;
  Color right = Colors.green;
  Color wrong = Colors.red;
  int marks = 0;
  int i = 1;

  // String videoURL ="https://www.youtube.com/watch?v=2OAdfB2U88A&t=593s";
  YoutubePlayerController _controller;


  // Use like this to make use of your array mydata
  String videoURL = widget.myData[4]["1"];

  @override
  void initState() {
    _controller = YoutubePlayerController(
        initialVideoId: YoutubePlayer.convertUrlToId(videoURL));
    super.initState();
  }
}

另外,这是出于编码的观点。请遵循 Flutter 中正确的类命名方式。始终使用 CamelCase将班级的第一个字母作为大写字母。这是您编写代码时的最佳实践。我希望以上内容对您有所帮助。谢谢:)

【讨论】:

  • 非常感谢你的演唱会,但我仍然很困惑,因为我不知道应该把 [mydata[4][i.toString()];] 放在哪里。当我将它插入 videoURL 时,它会产生错误。此外,当我将其插入 initialVideoId: YoutubePlayer.convertUrlToId());它也不起作用。我该怎么办
  • 嘿@MontasserMohamed,我已经编辑了代码。检查我已对 videoURL 数据进行了更改。看看,如果这对你有用
  • 谢谢。我按照您的步骤操作,但仍然会引发相同的错误。如果您能帮助我,我还在我的问题中的代码中添加了更多详细信息。
  • 在使用我更新的代码或不同的代码后,您是否得到与 [只有静态成员可以在初始化程序中访问] 相同的错误?如果不同,则表示您的问题已得到解答,如果您想要其他内容,则应更改问题
  • 是的,先生。我现在在小部件下仍然收到相同的错误,当我删除它时,错误返回到 videoURL
猜你喜欢
  • 2020-01-05
  • 2020-01-03
  • 2019-01-23
  • 2018-11-13
  • 2020-08-31
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多