Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

_TypeError (type 'int' is not a subtype of type 'double?')

I’m facing this issue on flutter… The error is –

_TypeError (type ‘int’ is not a subtype of type ‘double?’)

here is the code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 import 'package:flutter/material.dart';

 CustomButton(height,width,radius,buttonText,{color}){
  return Container(
    height: height,
    width: width,
    decoration: BoxDecoration(
      borderRadius: BorderRadius.circular(radius),
      color: color,
      border: Border.all(
        color: Colors.grey
      )
    ),
    child: Text(buttonText),
  );
}

>Solution :

You should indicate the correct types of the parameters of your function, like

CustomButton(double? height, double? width, double radius, String buttonText, {Color? color}){

Some extra information:

Not indicating a type makes it a dynamic. When you pass a number to a dynamic variable it automatically turns it into an int. Your code then tries to pass that int to the constructor of Container as width for example but that one requires a double?. So that will cause an error. By stating your parameter is a double? as well you make sure that any numbers passed to it will be doubles. Consider this example:

void main() {
    test1(1);
    test2(1);
}

void test1(i) {
  print(i.runtimeType);
}

void test2(double? i) {
  print(i.runtimeType);
}

test1 prints int and test2 prints double

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading