0

Please look at my sample code:

  @override
  Widget build(BuildContext context) {
   final selectedIssue = context.select((IssueDetailsBloc bloc) => bloc.state.selectedIssue) ?? InternalIssue();

    return WillPopScope(
      onWillPop: () async {
        final issuesScheduleBloc = context.read<IssuesScheduleBloc>();
        issuesScheduleBloc.add(UpdateIssueEvent(id: selectedIssue.id ?? 0, newData: selectedIssue));
        return true;
      },
      child: Scaffold(
        appBar: AppBar(title: const Text('Issue details')),
        body: Column(
          children: [
            Text(selectedIssue.title ?? ''), //<==== correct value is displayed
            TextFormField(initialValue: selectedIssue.title ?? '' //<==== selectedIssue.title is always null
            , onChanged: (text) {
              selectedIssue.title = text;
            },)
          ],
        ),
      ),
    );
  }

I want to be able to edit my current issue value. To display current value for edit, initialValue property has been set to selectedIssue.title. Problem: as long as selectedIssue.title is used to display static text, all is fine. But every time I try to use it as initial value for text form field, value is reverted to null.

Any tips how to do it correctly?

user1209216
  • 7,404
  • 12
  • 60
  • 123

1 Answers1

0

define a controller in the start of your file like this

TextEditingController controller = TextEditingController();

give your text field this controller like this

 TextFormField(
   controller: controller,
 )

then in your init state give the controller a text like this

 @override
  void initState() {
    controller.text = "your initial text";
  }