11

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'),
),
markhorrocks
  • 1,199
  • 19
  • 82
  • 151

2 Answers2

33

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

Philipos D.
  • 2,036
  • 1
  • 26
  • 33
9

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)."

Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40
miguelik
  • 475
  • 4
  • 11