flutter开发警告This class (or a class that this class inherits from) is marked as ‘@immutable‘, but one

这篇具有很好参考价值的文章主要介绍了flutter开发警告This class (or a class that this class inherits from) is marked as ‘@immutable‘, but one。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

this class (or a class that this class inherits from) is marked as '@immutab,Flutter,flutter,immutable,final,dart

问题描述

This class (or a class that this class inherits from) is marked as ‘@immutable’, but one or more of its instance fields aren’t final: SerialsTimer.tasks
this class (or a class that this class inherits from) is marked as '@immutab,Flutter,flutter,immutable,final,dart

问题代码

class SerialsTimer extends StatefulWidget {
  late Queue <Task> tasks; // 使用 Queue 来管理任务

  SerialsTimer({
    Key? key,
    required this.tasks,
  }) : super(key: key);

  
  State<SerialsTimer> createState() => _SerialsTimerState();
}

问题原因

Dart推断出你写的这个类是一个@immutable(不可变)的类。
这个警告是因为在一个被标记为 @immutable(不可变)的类中,其中的某个实例字段(成员变量)没有被声明为 final。

在Dart中,如果你使用了 @immutable 注解,它要求类的所有实例字段都应该是 final 的,以确保对象的不可变性。不可变对象的创建和管理有助于编写更加可靠和可维护的代码。

如何解决

为了解决这个警告,你可以在声明实例字段时将其标记为 final。

添加了 final 修饰符来修复 SerialsTimer 类中的 tasks 字段。这样,该类就符合 @immutable 的要求了。请确保在其他字段也符合相应的规定。

修改后的源码

class SerialsTimer extends StatefulWidget {
  final Queue <Task> tasks; // 使用 Queue 来管理任务

  const SerialsTimer({
    Key? key,
    required this.tasks,
  }) : super(key: key);

  
  State<SerialsTimer> createState() => _SerialsTimerState();
}

this class (or a class that this class inherits from) is marked as '@immutab,Flutter,flutter,immutable,final,dart文章来源地址https://www.toymoban.com/news/detail-764298.html


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

到了这里,关于flutter开发警告This class (or a class that this class inherits from) is marked as ‘@immutable‘, but one的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue js 回调函数 异步处理 为什么要 let that = this

    1 异步就是开个事务( 只有主线程 等主线程空闲 ),用that 值 做处理,然后返回处理结果,而that的值是开启 事务那一刻 的this的值.而在主线程处理的时候,this的一直在变化, that的值保留在那一刻 ps 或是将本obj 传递给其他的obj使用处理 ps 开启新事务或开启新子线程都是 在新的ob

    2024年02月11日
    浏览(42)
  • 【ES6】Class中this指向

    先上代码: 正常运行的代码: 输出: 单独调用函数printName: 输出: debugger调试一下,看看什么情况,调试代码: 调试界面,this显示undefined,在单独调用时,this的指向是undefined。在单独调用的场景下,要如何才能解决该问题呢?下面给出两种种比较简单的解决方法。 1、在

    2024年02月09日
    浏览(26)
  • 对于LayoutInflater.from(this).inflate()方法的理解

    对于 LayoutInflater.from(this).inflate() 方法的几个参数以及用法总是迷迷糊糊,源码看了忘,忘了看,因此决定写这篇博客做下记录。 我们知道,调用LayoutInflater.from(this).inflate()方法最终都会走三参的方法 public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) ,这

    2024年02月13日
    浏览(31)
  • 解决Docker报错:This error may indicate that the docker daemon is not running.

           成功安装完docker【安装过程见上一篇Windows安装docker_up_xin的博客-CSDN博客】后的第二天就遇到了问题,进入PowerShell后报错如下:       网上常见的解决方法是在Service Windows GUI重启docker desktop service,或者使用 DockerCli.exe -SwitchDaemon命令,但是都没能解决         网

    2024年02月11日
    浏览(38)
  • c++ std::enable_shared_from_this作用

    std::enable_shared_from_this 是一个类模板,用来返回指向当前对象的shared_ptr智能指针。在说明它的作用前我们可以看一下如下代码: demo.cpp 类 A 中有一个函数 getSharedPtr() 函数,用来返回指向当前对象的一个shared智能打针。其实就是用 this 构造了一个智能指针进行返回,执行结果

    2023年04月23日
    浏览(34)
  • Make the enclosing method “static“ or remove this set

    sonar安全扫描会报: Make the enclosing method “static” or remove this set.Why is this an issue? 修改成以下方式就可以了:

    2024年02月14日
    浏览(27)
  • error during connect: This error may indicate that the docker daemon is not running.: Get “http://%2

    error during connect: This error may indicate that the docker daemon is not running.: Get \\\"http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/images/search?limit=25term=java\\\": open //./pipe/docker_engine: The system cannot find the file specified. Windows解决方法: 忘记开启你的Docker Desktop了,开启来后,重新走一遍命令即可。  

    2024年02月08日
    浏览(41)
  • Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file

    maven打包报错 java 运行时的最新版本(类文件版本 61.0)编译的,该版本的 Java 运行时只识别 52.0 以下的类文件版本 原因: 根据 Spring Boot with spring version 2.5.7 fails repackage with jdk 1.8 这篇文章中下面的说法 目标org.springframework.boot:spring-boot-maven插件:3.0.0-M1:重新打包失败:由于

    2024年02月13日
    浏览(32)
  • Visual studio解决‘scanf: This function or variable may be unsafe. 问题

    使用C语言的scanf函数在Visual Studio软件上运行会报如下错误: \\\'scanf: This function or variable may be unsafe. Consider using scanf s instead. To disable deprecation, use. CRT SECURE NO WARNINGS. See online help for details. 这个函数或变量可能是不安全的。请考虑使用扫描S。若要禁用弃用,请使用。CRT没有任何警

    2024年02月09日
    浏览(36)
  • npm install 报错 this command with --force, or --legacy-peer-deps

    运行  npm install --legacy-peer-deps  紧接 运行 npm run serve 报错如下 然后针对这个报错,1.删除这个 vue/compiler-sfc ,又重装,2.删除整个node_modules,又初始化,3.安装更新过 vue-loader 的版本 4.将 npm 改成 6版本再运行   等等都无效 最后再此正式思考第一个报错,    尝试运行  

    2024年02月11日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包