如何修复此错误:偏移参数包含 NaN 值。 'dart:ui/painting.dart':断言失败:第 43 行:'<优化输出>'

Posted

技术标签:

【中文标题】如何修复此错误:偏移参数包含 NaN 值。 \'dart:ui/painting.dart\':断言失败:第 43 行:\'<优化输出>\'【英文标题】:How do I fix this error: Offset argument contained a NaN value. 'dart:ui/painting.dart': Failed assertion: line 43: '<optimized out>'如何修复此错误:偏移参数包含 NaN 值。 'dart:ui/painting.dart':断言失败:第 43 行:'<优化输出>' 【发布时间】:2021-01-13 13:23:41 【问题描述】:

我正在尝试创建一个带有颤动工具提示的时间序列图表,但我无法克服此错误消息。 “偏移量参数包含一个 NaN 值。'dart:ui/painting.dart':断言失败:第 43 行:''”。 https://github.com/google/charts/issues/58 是我从中获得此代码的地方,但似乎没有其他人有这个问题。尝试在该 github 页面上找到的其他类型的图表和不同的代码块时,总是会出现相同的错误消息。以下是相关代码:

import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';
import 'dart:math';
import 'package:charts_flutter/flutter.dart';
import 'package:charts_flutter/src/text_element.dart' as text;
import 'package:charts_flutter/src/text_style.dart' as style;

class CustomMeasureTickCount extends StatelessWidget 
  final List<charts.Series> seriesList;
  final bool animate;
  static String pointerValue;
  CustomMeasureTickCount(this.seriesList, this.animate);

  factory CustomMeasureTickCount.withSampleData() 
    return new CustomMeasureTickCount(
      _createSampleData(),
      animate: false,
    );
  

  @override
  Widget build(BuildContext context) 
    return new charts.TimeSeriesChart(seriesList,
        animate: animate,
        behaviors: [
          LinePointHighlighter(symbolRenderer: CustomCircleSymbolRenderer())
        ],
        selectionModels: [
          SelectionModelConfig(changedListener: (SelectionModel model) 
            if (model.hasDatumSelection)
              pointerValue = model.selectedSeries[0]
                  .measureFn(model.selectedDatum[0].index)
                  .toString();
          )
        ],

        /// Customize the measure axis to have 10 ticks
        primaryMeasureAxis: new charts.NumericAxisSpec(
            tickProviderSpec:
                new charts.BasicNumericTickProviderSpec(desiredTickCount: 10)));
  

  /// Create one series with sample hard coded data.
  static List<charts.Series<MyRow, DateTime>> _createSampleData() 
    final data = [
      new MyRow(new DateTime(2017, 9, 25), 6),
      new MyRow(new DateTime(2017, 9, 26), 8),
      new MyRow(new DateTime(2017, 9, 27), 6),
      new MyRow(new DateTime(2017, 9, 28), 9),
      new MyRow(new DateTime(2017, 9, 29), 11),
      new MyRow(new DateTime(2017, 9, 30), 15),
      new MyRow(new DateTime(2017, 10, 01), 25),
      new MyRow(new DateTime(2017, 10, 02), 33),
      new MyRow(new DateTime(2017, 10, 03), 27),
      new MyRow(new DateTime(2017, 10, 04), 31),
      new MyRow(new DateTime(2017, 10, 05), 23),
    ];

    return [
      new charts.Series<MyRow, DateTime>(
        id: 'Cost',
        domainFn: (MyRow row, _) => row.timeStamp,
        measureFn: (MyRow row, _) => row.cost,
        data: data,
        colorFn: (_, __) => charts.MaterialPalette.indigo.shadeDefault,
      )
    ];
  


/// Sample time series data type.
class MyRow 
  final DateTime timeStamp;
  final int cost;
  MyRow(this.timeStamp, this.cost);


class CustomCircleSymbolRenderer extends CircleSymbolRenderer 
  @override
  void paint(ChartCanvas canvas, Rectangle bounds,
      List dashPattern,
      Color fillColor,
      FillPatternType fillPattern,
      Color strokeColor,
      double strokeWidthPx) 
    super.paint(canvas, bounds,
        dashPattern: dashPattern,
        fillColor: fillColor,
        fillPattern: fillPattern,
        strokeColor: strokeColor,
        strokeWidthPx: strokeWidthPx);
    canvas.drawRect(
        Rectangle(bounds.left - 5, bounds.top - 30, bounds.width + 10,
            bounds.height + 10),
        fill: Color.white);
    var textStyle = style.TextStyle();
    textStyle.color = Color.black;
    textStyle.fontSize = 15;
    canvas.drawText(
        text.TextElement(CustomMeasureTickCount.pointerValue, style: textStyle),
        (bounds.left).round(),
        (bounds.top - 28).round());
  


【问题讨论】:

【参考方案1】:

发生错误是因为在文本字段处于焦点时发生了新事件,例如导航到新页面。要解决此问题,只需从文本字段中移除焦点即可。

例如 FocusScope.of(context).requestFocus(new FocusNode());

【讨论】:

这是解决问题的直接方法...thnx

以上是关于如何修复此错误:偏移参数包含 NaN 值。 'dart:ui/painting.dart':断言失败:第 43 行:'<优化输出>'的主要内容,如果未能解决你的问题,请参考以下文章

当我尝试使用 Laravel 中的“别名”从数据库中获取值时如何修复“未定义的偏移量:0”

如何修复错误:不能将字符串偏移量用作数组

如何修复此 Access 2003 错误?数据输入自动生成值

我该如何修复这个 NaN

错误 此版本的 node/NAN/v8 需要 C++11 编译器

错误 此版本的 node/NAN/v8 需要 C++11 编译器