Reputation: 43
i make a custom widget and using this widget in 7-8 place. i need to add a mask but in widget source said "it cant be null". i dont want to sent null parameter in all usage because i need to use only one place
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class TextFieldWidget extends StatelessWidget {
final text;
final controller;
final validation;
final mask;
final bool maxLength;
const TextFieldWidget({
Key? key,
this.text,
this.validation,
this.controller,
required this.maxLength, this.mask,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return TextFormField(
inputFormatters: [
LengthLimitingTextInputFormatter(maxLength ? 20 : null),
mask
],
validator: validation,
decoration: InputDecoration(
labelText: text,
border: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.grey, width: 0.0),
borderRadius: BorderRadius.circular(10)),
),
);
}
}
i need to be nullable this "mask" parameter. thanks all!
Upvotes: 0
Views: 57
Reputation: 4666
It's a good practice assigning the types of parameters instead of leaving them dynamic
as you do. A simple workaround is below :
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class TextFieldWidget extends StatelessWidget {
final text;
final controller;
final validation;
final TextInputFormatter? mask;
final bool maxLength;
const TextFieldWidget({
Key? key,
this.text,
this.validation,
this.controller,
required this.maxLength,
this.mask,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return TextFormField(
inputFormatters: [
LengthLimitingTextInputFormatter(maxLength ? 20 : null),
if (mask != null) mask!
],
validator: validation,
decoration: InputDecoration(
labelText: text,
border: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.grey, width: 0.0),
borderRadius: BorderRadius.circular(10)),
),
);
}
}
Upvotes: 3