0

For the sake of this question I have simplified my case here. I want to reuse a button widget which opens up a textfield dialog. If the button receive String data value then use text keyboard and if data type is int then use number keyboard. How can I do that? Here is my code

my screen

class MyScreen1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
 return Container(
   child: ReuseButton(value: 'pp'), //passing String value
  );
 }
}

reusable button

class ReuseButton extends StatelessWidget {
final dynamic value; // receiving unspecified data type
const ReuseButton({this.value});
@override
Widget build(BuildContext context) {
  return FlatButton(
    child: Text('Enter Value'),
    onPressed: () async {
      String s = await textFieldDialog(
          context: context,
          initialValue: '',
          keyboardType: TextInputType.numberWithOptions(
              decimal: true), //here to do comparison
          title: 'Equal to');
      print(s);
    },
  );
 }
}

I have already tried to make a model which is a simple class

class SpecVal<T> {
T data;
SpecVal(this.data);
}

and then pass this data in my screen to the button widget

ReuseButton(value: SpecVal<String>('some value'))

But then I was having problem to find out what data type SpecVal is using in the button widget to do comparison

LonelyWolf
  • 4,164
  • 16
  • 36

1 Answers1

1

You can check the type of a variable using is.

void main() {
  printType(42);             // 42 is an int
  printType("some string");  // some string is a String
}

void printType(dynamic value) {
  if (value is int)
    print("$value is an int");
  if (value is String)
    print("$value is a String");
}

More generally, you can retrieve the type of a variable with value.runtimeType.

void main() {
  printType(42);             // 42 is an int
  printType("some string");  // some string is a String
}

void printType(dynamic value) {
  Type type = value.runtimeType;
  if (type == int)
    print("$value is an int");
  if (type == String)
    print("$value is a String");
}

The type parameter of a generic function/class is also a Type so you can check it like above.

void main() {
  printType<String>();  // T is a Type / the value of T is String
}

void printType<T>() {
  if (T is Type)
    print("T is a Type");
  if (T == int)
    print("the value of T is int");
  if (T == String)
    print("the value of T is String");
}

There are many ways you can use these techniques on your problem, e.g. you can change your SpecVal to include a function like below:

class SpecVal<T> {
  ...
  bool get isString => T == String;
  ...
}

Related to How to perform runtime type checking in Dart?

Edman
  • 5,335
  • 29
  • 32
  • Hi Edman. Vote up for the 'is'. I didn't know about it. Thanks.. However I also might pass a null value where I only want specify data type when passing the value. Sorry for not being very clear... I tried it (is) with my model above SpecVal which works perfect if there is some value but when the value is null then it doesn't work – LonelyWolf Mar 04 '20 at 14:37
  • I updated the answer for finding the type of a parameter `T`. Let me know if that solves your problem. – Edman Mar 04 '20 at 16:51
  • That is what I was looking for.. Thanks – LonelyWolf Mar 05 '20 at 04:39