Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter undefined name WhitelistingTextInputFormatter

Tags:

flutter

I'm trying to use WhitelistingTextInputFormatter in a TextFormField in a Flutter app but I get

undefined name WhitelistingTextInputFormatter

Here is my code:

child: TextFormField(
         keyboardType: TextInputType.number,
         inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
         decoration: InputDecoration(labelText: 'Staff Number', hintText: 'enter staff number'),
),
like image 294
markhorrocks Avatar asked Feb 15 '26 15:02

markhorrocks


2 Answers

Even though you have package:flutter/services.dart imported, in Flutter 2.8.0 was fully deprecated.
Please check the documentation:

@Deprecated(
  'Use FilteringTextInputFormatter.digitsOnly instead. '
  'This feature was deprecated after v1.20.0-1.0.pre.',
)

Just change WhitelistingTextInputFormatter to FilteringTextInputFormatter

like image 175
Philipos D. Avatar answered Feb 18 '26 04:02

Philipos D.


can you confirm you are importing:

  import 'package:flutter/services.dart';

at the start of your file?

Also, i think here is the answer to your question:

How to use InputFormatter on Flutter TextField?

"In the services library you will find the TextInputFormatter abstract class (this means that you have to import package:flutter/services.dart)."

like image 33
miguelik Avatar answered Feb 18 '26 04:02

miguelik