1

My questions relates to this post: Flutter const with const constructors

I've got an issue trying to get rid of the blue squiggly lines under the majority of my widgets plus the MyApp class which I've never seen before. I prefix all the widgets with the const constructor but the issue just shifts to a different area of my code. I don't have this issue when coding in a different project so think it might be something outside the main.dart file.

Screenshots below illustrate errors and my fix attempts.

void main() => runApp(MyApp());


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        backgroundColor: Colors.teal[900],
        body:  SafeArea(
          child: Center(
            child:  Row(
              children: [
                Expanded(
                  child: Image(
                    image: AssetImage('images/dice1.png'),
                  ),
                ),
                SizedBox(
                  width: 20,
                ),
                Expanded(
                  child: Image(
                    image: AssetImage('images/dice1.png'),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

enter image description here

enter image description here

enter image description here

James B
  • 37
  • 5
  • Check this post https://stackoverflow.com/a/69310819/12413404 – esentis Dec 10 '21 at 11:04
  • so possible duplicate of [Flutter 2.5 update - const keyword on every static Widget](https://stackoverflow.com/questions/69309972/flutter-2-5-update-const-keyword-on-every-static-widget) – underscore_d Dec 11 '21 at 16:40
  • sadly the fix mentioned in that question didn't fix my issue. – James B Dec 11 '21 at 17:23

2 Answers2

1

You should put const keyword possibly highest in the widget tree. So if you've got three constant children inside a list (in a Row in your example), the whole list is a const itself.

If all children inside a list are constant, put const keyword right before the list.

So instead of this:

Row(
  children: [
    const Child1(),
    const Child2(),
    ...
  ],
)

Do this:

Row(
  children: const [
    Child1(),
    Child2(),
    ...
  ],
)

For more information see: https://dart.dev/guides/language/effective-dart/usage#dont-use-const-redundantly

FirentisTFW
  • 156
  • 6
  • @FirentisTWF I did try this too but I didn't attach a screenshot of the error. I've updated my answer. Thanks for the help. – James B Dec 11 '21 at 15:59
  • Please read my answer carefully once again :) Put the `const` keyword in front of the list (after `children:`), not in front of the `Row` as it doesn't have a const constructor. – FirentisTFW Dec 11 '21 at 19:57
  • Oops, sorry! Thanks for the answer – James B Dec 12 '21 at 11:41
0

I have a simple solution to avoid const constructor. Go in your analysis_options.yaml file, and put this line at the bottom of the rule section

rule: 
  prefer_const_construtors: false,
Caesar
  • 6,733
  • 4
  • 38
  • 44