簡體   English   中英

flutter error : RangeError (index): Invalid value: 有效值范圍為空:0

[英]flutter error : RangeError (index): Invalid value: Valid value range is empty: 0

我從github上拉的項目flutter運行成功
但是頁面沒有畫出一波數據

1

  (elided 3 frames from dart:async)
The following RenderObject was being processed when the exception was fired: RenderClipPath#2d7dd relayoutBoundary=up5
RenderObject: RenderClipPath#2d7dd relayoutBoundary=up5
    parentData: offset=Offset(0.0, 0.0) (can use size)
    constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=674.9)
    size: Size(392.7, 392.7)
    child: RenderConstrainedBox#cc534 relayoutBoundary=up6 NEEDS-PAINT
        parentData: <none> (can use size)
        constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=674.9)
        size: Size(392.7, 392.7)
        additionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=392.7)
            gradient: LinearGradient(begin: Alignment.centerLeft, end: Alignment.centerRight, colors: [Color(0xfffeac5e), Color(0xffc779d0), Color(0xff4bc0c8)], stops: [0.1, 0.3, 0.9], tileMode: TileMode.clamp)
        configuration: ImageConfiguration(bundle: PlatformAssetBundle#2c6f4(), devicePixelRatio: 2.8, locale: en_US, textDirection: TextDirection.ltr, platform: android)
        child: RenderLimitedBox#7ca4f relayoutBoundary=up8 NEEDS-PAINT
            parentData: <none> (can use size)
            constraints: BoxConstraints(0.0<=w<=392.7, h=392.7)
            size: Size(392.7, 392.7)
            maxWidth: 0.0
            maxHeight: 0.0
            child: RenderConstrainedBox#bb55f relayoutBoundary=up9 NEEDS-PAINT
                parentData: <none> (can use size)
                constraints: BoxConstraints(0.0<=w<=392.7, h=392.7)
                size: Size(392.7, 392.7)
                additionalConstraints: BoxConstraints(biggest)

這是我的 CustomPaint 和 ClipPath

════════ Exception caught by rendering library ═════════════════════════════════
RangeError (index): Invalid value: Valid value range is empty: 0
The relevant error-causing widget was
CustomPaint and   ClipPath

 children: <Widget>[
                CustomPaint(
                  size: Size(
                    constraints.maxWidth,
                    height,
                  ),
                  foregroundPainter: WaveformPainter(
                    widget.sampleData,
                    zoomLevel: zoomLevel,
                    startingFrame: widget.sampleData
                        .frameIdxFromPercent(startPosition),
                    color: Color(0xff3994DB),
                  ),
                ),

            ClipPath(
                      clipper: WaveformClipper(snapshot.data!),
                      child: Container(
                        height: height,
                        decoration: const BoxDecoration(
                          gradient: LinearGradient(
                            begin: Alignment.centerLeft,
                            end: Alignment.centerRight,
                            stops: [0.1, 0.3, 0.9],
                            colors: [
                              Color(0xffFEAC5E),
                              Color(0xffC779D0),
                              Color(0xff4BC0C8),
                            ],
                          ),
                        ),
                      ),
                    );

這是我的顫振醫生

[√] Flutter (Channel stable, 3.0.0, on Microsoft Windows [10.0.19044.1706], locale zh-Hans-HK)
[√] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2021.2)
[√] Connected device (3 available)
[√] HTTP Host Availability

這是我的 WaveformData._scaleData

  _scaleData() {
    final max = pow(2, bits! - 1).toDouble();
    final dataSize = data?.length;
    _scaledData = [];
    for (var i = 0; i < dataSize!; i++) {
      _scaledData![i] = data![i].toDouble() / max;
      if (_scaledData![i] > 1.0) {
        _scaledData![i] = 1.0;
      }
      if (_scaledData![i] < -1.0) {
        _scaledData![i] = -1.0;
      }
    }
  }
}

這是我的 WaveformData.path

 Path path(Size size, {zoomLevel = 1.0, int fromFrame = 0}) {
    if (!_isDataScaled()) {
      _scaleData();
    }

這是我的 WaveformClipper.getClip

lass WaveformClipper extends CustomClipper<Path> {
  WaveformClipper(this.data);

  final WaveformData? data;

  @override
  Path getClip(Size size) {
    return data!.path(size);
  }

  @override
  bool shouldReclip(WaveformClipper oldClipper) {
    if (data != oldClipper.data) {
      return true;
    }
    return false;
  }

}

您需要在此處進行空檢查。 可能 dataSize 為空。 你需要這樣做。

  if(dataSize != null){
for (var i = 0; i < dataSize!; i++) {
      _scaledData![i] = data![i].toDouble() / max;
      if (_scaledData![i] > 1.0) {
        _scaledData![i] = 1.0;
      }
      if (_scaledData![i] < -1.0) {
        _scaledData![i] = -1.0;
      }
    } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM