I am trying to make a TextFormField that formats a phone number as you type it. I am trying to use the libphonenumber package to do this, but the method it provides returns a Future<String>, not a String. I can't figure out how to make it work.
My input field looks like this:
TextFormField(
decoration: InputDecoration(
hintText: "Phone Number",
),
controller: _textController,
keyboardType: TextInputType.phone,
inputFormatters: <TextInputFormatter>[_PhoneFormatter('US')],
)
and the formatter code looks like this:
import 'package:libphonenumber/libphonenumber.dart';
class _PhoneFormatter extends TextInputFormatter {
final String _countryCode;
_PhoneFormatter(this._countryCode);
@override
TextEditingValue formatEditUpdate(oldPhone, newPhone) {
final phoneNumber = PhoneNumberUtil.formatAsYouType(
phoneNumber: newPhone.text, isoCode: _countryCode);
return TextEditingValue(
text: phoneNumber,
selection: TextSelection.collapsed(offset: phoneNumber.length));
}
}
but I get the error:
The argument type
'Future<String'can't be assigned to the parameter type 'String'.
I tried to re-write it with "async/await":
@override
TextEditingValue formatEditUpdate(oldPhone, newPhone) async {
final phoneNumber = await PhoneNumberUtil.formatAsYouType(
phoneNumber: newPhone.text, isoCode: _countryCode);
return TextEditingValue(
text: phoneNumber,
selection: TextSelection.collapsed(offset: phoneNumber.length));
}
but I then get the error:
Functions marked 'async' must have a return type assignable to 'Future'.
How can I make this work?