flutter中的null上调用了方法'toDouble()'

时间:2020-05-03 15:50:13

标签: flutter flutter-layout

因此,我尝试获取类似this as in the library的按钮,因此我尝试使该按钮像在该库中一样完成,但出现错误,我尝试查找原因,发现它在我不这样做时发生我在double值中定义了一些值,所以我这样做了,但同时我也遇到了同样的错误。

ui_helper.dart

import 'dart:core';

/// ui standard
final standardWidth = 375.0;
final standardHeight = 815.0;

/// late init
double screenWidth;
double screenHeight;

/// scale [height] by [standardHeight]
double realH(double height) {
assert(screenHeight != 0.0);
return height / standardHeight * screenHeight;
}

// scale [width] by [ standardWidth ]
double realW(double width) {
 assert(screenWidth != 0.0);
 return width / standardWidth * screenWidth;
}

MapButton.dart

class MapButton extends StatelessWidget {
final double currentSearchPercent;
final double currentExplorePercent;

 final double bottom;
final double offsetX;
final double width;
final double height;

final IconData icon;
final Color iconColor;
final bool isRight;
final Gradient gradient;

const MapButton(
  {Key key,
    this.currentSearchPercent,
    this.currentExplorePercent,
    this.bottom,
    this.offsetX,
    this.width,
    this.height,
    this.icon,
    this.iconColor,
    this.isRight = true,
    this.gradient})
  : assert(currentExplorePercent != null),
    assert(currentExplorePercent != null),
    assert(bottom != null),
    assert(offsetX != null),
    assert(width != null),
    assert(height != null),
    assert(icon != null),
    super(key: key);

@override
Widget build(BuildContext context) {
return Positioned(
  bottom: realH(bottom),
  left: !isRight ? realW(offsetX * (currentExplorePercent + currentSearchPercent)) : null,
  right: isRight ? realW(offsetX * (currentExplorePercent + currentSearchPercent)) : null,
  child: Opacity(
    opacity: 1 - (currentSearchPercent + currentExplorePercent),
    child: Container(
      width: realW(width),
      height: realH(height),
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(left: realW(17)),
      child: Icon(
        icon,
        size: realW(34),
        color: iconColor ?? Colors.black,
      ),
      decoration: BoxDecoration(
          color: gradient == null ? Colors.white : null,
          gradient: gradient,
          borderRadius: isRight
              ? BorderRadius.only(bottomLeft: Radius.circular(realW(36)), topLeft: Radius.circular(realW(36)))
              : BorderRadius.only(bottomRight: Radius.circular(realW(36)), topRight: Radius.circular(realW(36))),
          boxShadow: [
            BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.3), blurRadius: realW(36)),
          ]),
    ),
  ),
);
}
}

这些是我从该库中使用的类,但到目前为止还无法正常工作。 我这样称呼

MapButton(
        currentExplorePercent: currentExplorePercent,
        currentSearchPercent: currentSearchPercent,
        bottom: 243.0,
        offsetX: -71.0,
        width: 71.0,
        height: 71.0,
        isRight: false,
        icon: Icons.layers,
      ),

我试图运行该库,但运行正常。任何帮助如何解决此问题。

1 个答案:

答案 0 :(得分:1)

主要问题是您没有提供screenWidth和screenHeight值。如果提供的话,将会解决您的问题。

调用小部件时,它将调用realH,但screenWidth的值未定义,因此它无法计算值,也不能返回值。

  screenWidth = MediaQuery.of(context).size.width; 
screenHeight = MediaQuery.of(context).size.height;
 if (screenWidth > standardWidth) { 
   screenWidth = standardWidth;
  } 
 if (screenHeight > standardHeight) { 
    screenHeight = standardHeight;
 }