Prefer const literals as parameters of constructors on @immutable classes.

This Article is posted by seven.srikanth at 11/26/2022 2:14:42 AM



I've got this problem while trying the below code. 

class MyWidget extends StatelessWidget {
  const MyWidget({
    super.key,
  });
  @ override
  Widget build(BuildContext context) {
    return Center(
      child: Wrap(
        spacing: 10.0,
        alignment: WrapAlignment.start,
        children: [
          const MyContainer(), //Error occured in this line
          const MyContainer(), //Error occured in this line
          const MyContainer(), //Error occured in this line
        ],
      ),
    );
  }
}
First of all this is not an error, and your program should work fine without resolving it too. But, no one likes to see some squiggly lines in their programs, right? 
However, the reason it occurred, is that all the widgets inside the List are immutable. Hence it suggests adding the const keyword in one place. 
In order to fix this issue, I have changed it as shown below.

class MyWidget extends StatelessWidget {
  const MyWidget({
    super.key,
  });
  @ override
  Widget build(BuildContext context) {
    return Center(
      child: Wrap(
        spacing: 10.0,
        alignment: WrapAlignment.start,
        children: const [
          MyContainer(),
          MyContainer(),
          MyContainer(),
        ],
      ),
    );
  }
}
Hope this is helpful.
Thanks,
Srikanth

Tags: flutter constant;








0 Comments
Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.


© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com