flutter - 在 flutter 中调用 null 方法 'toDouble()'

标签 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,
      ),

我尝试运行该库,它运行良好。任何帮助如何解决这个问题。

最佳答案

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

当你的widget调用时,它会调用realH,但screenWidth的值没有定义,所以它无法计算值,也无法返回值。

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

关于flutter - 在 flutter 中调用 null 方法 'toDouble()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61577333/

相关文章:

flutter - 如何将 <dynamic> 转换为 List<String>?

flutter - 尝试在 flutter 中创建不同语言的 pdf

flutter :- Keyboard causes layout to overflow on the bottom when on focus

flutter - 在这种情况下,如何在我点击_card时打开新路线/页面?

flutter - 如何更改 FloatingActionButton 的大小?

flutter - 当在Stream上使用Distinct()时似乎无法过滤出相同的结果

flutter - 当我在 Flutter 中打印 map 时,Flutter 中缺少双引号

Flutter TextFormField 初始值不起作用

flutter - 类型 'Null' 不是类型转换中类型 'String' 的子类型 - Flutter

flutter - 如何在GridView中增加卡片大小