flutter开发报错The instance member ‘widget‘ can‘t be accessed in an initializer

编程入门 行业动态 更新时间:2024-10-24 14:24:47

flutter开发<a href=https://www.elefans.com/category/jswz/34/1771188.html style=报错The instance member ‘widget‘ can‘t be accessed in an initializer"/>

flutter开发报错The instance member ‘widget‘ can‘t be accessed in an initializer

文章目录

  • 问题描述
  • 问题原因
  • 解决方法

问题描述

The instance member ‘widget’ can’t be accessed in an initializer.

问题原因

“The instance member ‘widget’ can’t be accessed in an initializer” 错误是因为在初始化器列表中(constructor initializer list)访问了 widget 成员。这是因为在构造函数的初始化器列表中,对象的属性(包括 widget)还没有被初始化,因此不能在这里访问它们。

有问题的源码

class CountdownTimer extends StatefulWidget {final int duration;final int numIterations;final int breakTime;const CountdownTimer({Key? key,required this.duration,required this.numIterations,required this.breakTime,}) : super(key: key);@overrideState<CountdownTimer> createState() => _CountdownTimerState();
}class _CountdownTimerState extends State<CountdownTimer> {final String countDownMusic='assets/beat.mp3';final String restMusic='assets/piano.mp3';late Timer _timer;double _timeProgress = 1.0;double _numProgress = 1.0;int rounds = widget.numIterations;

解决方法

int rounds = widget.numIterations;

这一句增加一个late修饰符

late int rounds = widget.numIterations;

结束语
Flutter是一个由Google开发的开源UI工具包,它可以让您在不同平台上创建高质量、美观的应用程序,而无需编写大量平台特定的代码。我将学习和深入研究Flutter的方方面面。从基础知识到高级技巧,从UI设计到性能优化,欢饮关注一起讨论学习,共同进入Flutter的精彩世界!

更多推荐

flutter开发报错The instance member ‘widget‘ can‘t be accessed in an initializer

本文发布于:2023-11-16 07:44:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1614618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:报错   instance   flutter   member   initializer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!