0

I found a problem, because I just learned the newest flutter, whereas what I learned was the old version of flutter.

when I make myList variable separate from Children

Errors:

Can't define the 'const' constructor because the field 'mylist' is initialized with a non-constant value.
Try initializing the field to a constant value, or removing the keyword 'const' from the constructor. [Ln 8, Col 3]

Can't define a const constructor for a class with non-final fields.
Try making all of the fields final, or removing the keyword 'const' from the constructor. [Ln 9, Col 9]

Script :

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  List<Widget> mylist = [
            Container(
              height: 300,
              width: 300,
              color: Colors.red,
            ),
            Container(
              height: 300,
              width: 300,
              color: Colors.green,
            ),
            Container(
              height: 300,
              width: 300,
              color: Colors.blue,
            ),
            Container(
              height: 300,
              width: 300,
              color: Colors.amber,
            ),
          ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("List View"),
        ),        
        body: ListView(
          //scrollDirection: Axis.horizontal,
          children: mylist,
        ),
      ),
    );
  }
}

Please help and explain the details of the problem and references

MendelG
  • 14,885
  • 4
  • 25
  • 52

2 Answers2

0

Remove the const keyword since MyApp() isn't a constant:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({super.key});
  List<Widget> mylist = [
    Container(
      height: 300,
      width: 300,
      color: Colors.red,
    ),
    Container(
      height: 300,
      width: 300,
      color: Colors.green,
    ),
    Container(
      height: 300,
      width: 300,
      color: Colors.blue,
    ),
    Container(
      height: 300,
      width: 300,
      color: Colors.amber,
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("List View"),
        ),
        body: ListView(
          //scrollDirection: Axis.horizontal,
          children: mylist,
        ),
      ),
    );
  }
}

For an explanation see: What is the difference between the "const" and "final" keywords in Dart?

If the value you have is computed at runtime (new DateTime.now(), for example), you can not use a const for it. However, if the value is known at compile time (const a = 1;), then you should use const over final. There are 2 other large differences between const and final. Firstly, if you're using const inside a class, you have to declare it as static const rather than just const. Secondly, if you have a const collection, everything inside of that is in const. If you have a final collection, everything inside of that is not final.

MendelG
  • 14,885
  • 4
  • 25
  • 52
  • Previously I tried to like that but the error was in widget_test.dart – Tri Amin Ridho Jan 02 '23 at 02:33
  • @TriAminRidho It's fine, you can just remove it from there too (or ignore it). See https://stackoverflow.com/questions/71651959/error-in-const-myapp-in-widget-test-dart-file – MendelG Jan 02 '23 at 02:44
  • thank you for helping, the program was successful there was no error but before debug there was a notification "Build errors exist in your project". but i still "debug anyway" it works. I was still not satisfied so I tried the previous version so I could learn with the appropriate instructions. Thank you – Tri Amin Ridho Jan 02 '23 at 03:54
0

Well, because the mylist is mutable it means that it can be changed, and re-assignable, and the class which contains it can't be const anymore.

a const class is a class whose members are also const ( constant ).

so if you're willing to re-assign the mylist value in your code, then you need to remove the const keyword from your class constructor like this:

class MyApp extends StatelessWidget {
  MyApp({super.key}); // removed const
    List<Widget> mylist = [
 // ...
Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
  • if I use 'final'. there is still a problem in the 'const' section error: Can't define the 'const' constructor because the field 'myList' is initialized with a non-constant value. – Tri Amin Ridho Jan 02 '23 at 02:40
  • sorry, you're right, using final won't solve it, since Container can't be const also, I didn't notice it. – Gwhyyy Jan 02 '23 at 02:41