0

I have a field called images in a class:

  final images = ["amusement1.jpeg", "amusement2.jpg", "amusement3.png", "amusement4.jpeg",
  "amusement5.jpeg", "amusement6.jpeg"];

And I want to set the default value of another field:

  var displayImage = images[0];

But this gives the error:

lib/main.dart:46:22: Error: Can't access 'this' in a field initializer to read 'images'.
  var displayImage = images[0];                                         
                     ^^^^^^                                             
Performing hot restart...                                           40ms

How to initialize a value of a field in Dart?

zendevil.eth
  • 974
  • 2
  • 9
  • 28
  • You are presumably declaring something like `List<...> images = ...;` before the declaration of `displayImage`, and you are doing this at the class field level (rather than within a function). You can't do this because `images[0]` is implicitly turned into `this.images[0]`, and `this` doesn't exist until after the constructor returns. – Abion47 May 21 '21 at 22:36

1 Answers1

0

If you are using latest version of dart (2.12 or above), you can use the late keyword.

Declare it as:

late var displayImage = images[0];

Or, If you are using Stateful widget, then you can initialise it in the initState method.

String displayImage;

@override
void initState() {
   super.initState();
   displayImage = images[0];
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • That gives: lib/main.dart:46:3: Error: Null safety features are disabled for this library. Try removing the package language version or setting the language version to 2.12 or higher. late var displayImage = images[0]; ^^^^ Performing hot restart... 52ms Try again after fixing the above error(s). – zendevil.eth May 21 '21 at 19:09
  • @lampbottle Which version of sdk you are using? – Midhun MP May 21 '21 at 19:09
  • Flutter 2.0.6 • channel stable • https://github.com/flutter/flutter.git Framework • revision 1d9032c7e1 (3 weeks ago) • 2021-04-29 17:37:58 -0700 Engine • revision 05e680e202 Tools • Dart 2.12.3 – zendevil.eth May 21 '21 at 19:27
  • @lampbottle As mentioned in the answer, late keyword introduced in 2.12, so you can initialise the variable in initState as shown in the later part of the answer – Midhun MP May 21 '21 at 21:00
  • @MidhunMP I think you wouldn't need `late` in your first code block , but you would need it in the second code block when declaring the variable like : `late String displayImage` – Calvin Gonsalves May 21 '21 at 22:04
  • `late` does nothing to solve the issue in the first example. The second example does address the issue, but needs `late` in order to work. – Abion47 May 21 '21 at 22:37
  • @CalvinGonsalves No, first code block is for dart 2.12 or above. When you try to assign a value in the class level itself (as shown in question, you need late keyword). 2nd code block is for normal situation (no null safety). Where you can declare a variable like that and initialise it in initState, I don't know why you said late is necessary there, did you tried that? – Midhun MP May 22 '21 at 13:57
  • @Abion47 Why you said late won't solve the issue? Did you tried that before commenting? (Because I did that, before posting it as an answer). In the second example late is not needed (if no null safety is there or older version of dart, or you can just set an empty string to initialise it). – Midhun MP May 22 '21 at 13:59
  • 1
    @MidhunMP you're right, Sorry for the confusion there. I thought the second code block was in null safety. – Calvin Gonsalves May 24 '21 at 01:22