Skip to content

Dart compile error

Flutter: “The instance member ‘…’ can’t be accessed in an initializer”

Dart refuses to compile with The instance member ‘widget’ (or ‘params’, ‘controller’ and so on) can’t be accessed in an initializer. A field’s starting value, or a constructor’s initializer list, reads another instance member before the object exists. In Flutter it most often happens in a State class, as final controller = TextEditingController(text: widget.initialText).

Rather hand it over? App Fix Ticket: One reproducible bug in an existing mobile or web app, diagnosed and fixed.The Fix is $1,200 in total. The $450 Diagnosis is optional and credited toward it.

Full refund if Simon can’t take it on once he sees your project, or if you cancel before work starts. Refund terms.

Short answer

Give the field its value where the object already exists. In a State class, assign it in initState, where widget is available. Or mark the field late so its initializer runs on first use (this works in a State class too, because widget is set by the time the field is first read), or move the value into the constructor body. If the value doesn’t depend on the instance, make the member it reads static.

Rather not fix it yourself? App Fix Ticket fixes one reproducible bug at a fixed price, with an optional Diagnosis that is credited toward the fix.

Why the initializer can’t see the member, and the fix

  1. 01

    A State field that reads widget

    Field initializers run before the State is attached to its widget, so widget isn’t available there. Declare the field late and assign it in initState, and dispose it in dispose if it is a controller.

    class _EditNameState extends State<EditName> {
      late final TextEditingController _controller;
    
      @override
      void initState() {
        super.initState();
        _controller = TextEditingController(text: widget.initialName);
      }
    
      @override
      void dispose() {
        _controller.dispose();
        super.dispose();
      }
    }
  2. 02

    A field that reads another field

    “When you mark a variable as late but initialize it at its declaration, then the initializer runs the first time the variable is used.” Dart, Variables: late variables

    In a plain class, final total = price * quantity fails the same way. Mark the field late: the initializer then runs the first time the field is read, when the object exists. Use a getter instead if the value should follow later changes.

    class Order {
      Order(this.price, this.quantity);
      final double price;
      final int quantity;
    
      late final double total = price * quantity; // runs on first use
      // or, always up to date:
      // double get total => price * quantity;
    }
  3. 03

    A constructor initializer list that calls a method

    “If the member can be made static, then do so” Dart, implicit_this_reference_in_initializer

    C() : x = defaultX fails when defaultX is an instance getter or method. If it doesn’t need the instance, make it static; otherwise compute the value in the constructor body or pass it in.

    class C {
      int x;
      C() : x = defaultX;
      static int get defaultX => 0;
    }

Checked against the vendor’s docs on : Dart diagnostics: implicit_this_reference_in_initializer; Dart: Variables (late variables).

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

“Very responsive, attentive to fixing bugs, and completed the project as needed! Thank you!”
A buyer in the United States, July 2022. Shopify on Fiverr
Read 126 of the 127 Fiverr reviews

Related: Flutter “The parameter can’t have a value of null” · Flutter “Invalid constant value” · Flutter setState() called during build · Flutter “The argument type ‘String’ can’t be assigned to the parameter type ‘Uri’”

Questions

More on this error

Why not use late everywhere?

A late field that is read before it is assigned throws a LateInitializationError at runtime instead of a compile error. In a State class, assign it in initState so it is always set before build runs.

Can I read widget in didChangeDependencies instead?

Yes, but it runs again whenever an inherited widget changes, so guard it if the value should be set once. initState is the usual place for values that depend only on widget.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it. If you already know the cause, you can pay for the fix directly; if not, start with a written diagnosis, which is credited toward the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Ask Simon for a price range

Send the error and what you were doing. Simon replies by email, usually within one business day, with a price range.