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

Time:10-13

I'm facing this issue on flutter... The error is -

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

here is the code :

 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),
  );
}

CodePudding user response:

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

  • Related