метод 'toDouble ()' был вызван на ноль в флаттере - PullRequest
0 голосов
/ 03 мая 2020

Итак, я пытаюсь получить кнопки типа , как в библиотеке , поэтому я попытался сделать кнопку как в этой библиотеке, но получаю сообщение об ошибке. Я попытался найти причину и обнаружил, что это происходит, когда я не определяю какое-либо значение в двойном значении, поэтому я сделал это, но затем я также получил ту же ошибку.

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 Ответ

1 голос
/ 03 мая 2020

Основная проблема заключается в том, что вы не указали значение 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;
 } 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...