Home > OS >  how can I successfully compile a Dart app when I get a "late" modifier error (Unexpected t
how can I successfully compile a Dart app when I get a "late" modifier error (Unexpected t

Time:05-10

I have the following line here (I am trying to add a flutter package which uses it):

class _PersonaWidgetState extends State<PersonaWidget> {
  late InquiryConfiguration _configuration;

 @override
   void initState() {
     super.initState();

   _configuration = TemplateIdConfiguration(
     templateId: "TEMPLATE_ID",
     environment: InquiryEnvironment.sandbox,
     fields: InquiryFields(
     name: InquiryName(first: "John", middle: "Apple", last: "Seed"),
      additionalFields: {"test-1": "test-2", "test-3": 2, "test-4": true},
     ),

Even though 'late' is an accepted modifier in dart, I get the following:

persona_widget.dart:26:3: Unexpected text 'late'.

I don't think there's a way around it since I need the functionality provided by "late" and wouldn't know what modifier to substitute for it.

This is the setting for the sdk in the pubspec.yaml:

environment:
  sdk: ">=2.7.0 <3.0.0"

Edit:

• Flutter version 2.10.5 at /Users/timfong/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5464c5bac7 (3 weeks ago), 2022-04-18 09:55:37 -0700
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

CodePudding user response:

With the introduction of NNBD (Non-Nullable By Default) in Dart 2.12, a new keyword was created: late. The primary reason this was created was to allow for non-null fields, that did not have to be immediately initialized.

Solution #1: Migrate the whole project to null safety. I think this is the best option as without null safety you'll be missing a lot of new features. For Null Safety migration: https://dart.dev/null-safety/migration-guide

Solution #2: You can just remove the late keyword.

from:

late InquiryConfiguration _configuration;

To

InquiryConfiguration _configuration;

CodePudding user response:

late is for projects converted to null safety using min dart sdk 2.12. It tells the compiler that it's null now but will be initialized later on. You can either omit the late keyword in that case or change the min sdk in your pubspec.yaml to 2.12.

  • Related