Bloc: TextFormField's initialValue from a cubit state

Created on 3 Aug 2020  路  4Comments  路  Source: felangel/bloc

Hi there,
First of all, thanks for this great library, it's making my whole dart/flutter experience way more productive!

I'm trying to initialise a TextFormField with the value provided from a state, but I'd like that initialValue to be updated if the state changes.
Unfortunately, though the BlocBuilder is being invoked when the state is updated, the UI doesn't reflect the change.

Am I missing something?

Thanks

import 'dart:math';

import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class MyState extends Equatable {
  final String value;

  const MyState(this.value);

  @override
  List<Object> get props => [value];
}

class MyCubit extends Cubit<MyState> {
  MyCubit() : super(MyState('initial'));

  void update(String newValue) => emit(MyState(newValue));
}

class TestRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<MyCubit, MyState>(builder: (context, state) {
      print("Building... [${state.value}]");
      return Scaffold(
        body: Column(
          children: [
            // I wish this to be updated, but it doesn't
            TextFormField(
              initialValue: state.value,
            ),
            // A regular Text widget is updated with no problem though
            Text(state.value),
            ElevatedButton(
              onPressed: () => context
                  .bloc<MyCubit>()
                  .update(Random.secure().nextDouble().toString()),
              child: Text('GO!'),
            )
          ],
        ),
      );
    });
  }
}

question

All 4 comments

Hi @prascuna 馃憢

Your text form field is not being updated because initialValue is only set once, as the name implies.
You'd have to make your widget stateful(to dispose the controller) and use a TextEditingController then you could simply do:

TextFormField(
  controller: textEditingController..text = state.value,
)

Thanks for your prompt reply @RollyPeres !
Somehow I thought that the UI should have been rebuilt, as the BlocBuilder.builder method was being invoked, therefore a new immutable TextFormField being returned.
Thanks a lot, I will use a controller then!

@prascuna the TextFormField is a StatefulWidget and will create it's own TextEdittingController if you don't provide one so even though builder is being invoked and a new TextFormField is returned, because it is Stateful it will retain the controller and the initialValue as @RollyPeres mentioned.

I see, that makes sense now. Thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abinvp picture abinvp  路  3Comments

frankrod picture frankrod  路  3Comments

clicksocial picture clicksocial  路  3Comments

MahdiPishguy picture MahdiPishguy  路  3Comments

nhwilly picture nhwilly  路  3Comments