0
0 Comments

I am trying to set an initial value if one is created on my text form field. When I run my code:

final apiField = TextFormField(
  controller: apiFieldController,
  initialValue: _read().toString(),
  obscureText: true,
  style: style,
  decoration: InputDecoration(
      contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
      hintText: "API Key",
      border:
          OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);

_read() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return String
String stringValue = prefs.getString('apiKey') ?? '';
return stringValue;
}

I get the following error:

package:flutter/src/material/text_form_field.dart: failed assertion. initial value == null || controller == null is not true

I’m unsure where I’m stuck at. I do see the controller being initialized. I believe my _read() method has an issue, but it does return a string.

Anonymous Asked question May 13, 2021