Expose controller in constructor to initialize with starting text
Closed this issue · 3 comments
CoreyCole commented
When using a normal TextField
, I can initialize the starting text:
Widget getAuthorField(Bloc bloc) {
return StreamBuilder(
stream: bloc.customAuthor,
builder: (context, AsyncSnapshot<String> snapshot) {
if (!snapshot.hasData) return SizedBox();
final author = snapshot.data;
authorController = TextEditingController.fromValue(
TextEditingValue(
text: author,
selection: new TextSelection.collapsed(offset: author.length)
)
);
return TextField(
controller: authorController
);
},
initialData: ''
);
}
I'm not able to do this with AutoCompleteTextField
:
Widget getAuthorField(Bloc bloc) {
return StreamBuilder(
stream: bloc.customAuthor,
builder: (context, AsyncSnapshot<String> snapshot) {
if (!snapshot.hasData) {
return SizedBox();
}
autoCompleteAuthorField = createAutoCompleteAuthorInput(context, bookList, bloc);
// both of these don't work inside the StreamBuilder
// authorInputKey.currentState.textField.controller.text = snapshot.data;
// autoCompleteAuthorField.textField.controller.text = snapshot.data;
return autoCompleteAuthorField;
},
initialData: '',
);
}
AutoCompleteTextField<ApprovedBook> createAutoCompleteTitleInput(BuildContext context, List<ApprovedBook> approvedBooks, Bloc bloc) {
return AutoCompleteTextField<ApprovedBook>(
key: titleInputKey,
textChanged: (text) {
bloc.changeCustomTitle(text);
},
decoration: storytimeInputDecoration('Title', Theme.of(context).accentColor, 'The title of the book', ''),
itemSubmitted: (ApprovedBook book) {
bloc.submitApprovedBook(book);
_resetForm();
Navigator.pushNamed(context, '/confirm_book');
},
suggestions: approvedBooks,
itemFilter: (suggestion, input) {
return suggestion.title.toLowerCase().startsWith(input.toLowerCase());
},
itemBuilder: (context, ApprovedBook suggestion) => Padding(
padding: EdgeInsets.all(8),
child: ListTile(
title: Text('${suggestion.title} by ${suggestion.author}')
)
),
itemSorter: (a, b) => a.title.compareTo(b.title),
);
}
I saw #11, where we can access the textField
property after the AutoCompleteTextField
has been built. But, that is not working for me using StreamBuilder
. I get error:
flutter: isAuthenticated? true
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown building StreamBuilder<String>(dirty, dependencies:
flutter: [_LocalizationsScope-[GlobalKey#4ef5a], _InheritedTheme], state: _StreamBuilderBaseState<String,
flutter: AsyncSnapshot<String>>#fe9e7):
flutter: The getter 'textField' was called on null.
flutter: Receiver: null
flutter: Tried calling: textField
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
flutter: #1 FindBookScreenState.getAuthorField.<anonymous closure>
xalikoutis commented
same problem here, i need a initial value
felixmccuaig commented
I'll have to look a bit deeper into this one, thanks for raising the issue though.
felixmccuaig commented
This one should be fixed now.