简体   繁体   English

Flutter:使用提供程序 package 在帧错误期间计划构建

[英]Flutter: Build scheduled during frame error using Provider package

Hey guys I need help in de-bugging what is going on.嘿伙计们,我需要帮助来调试正在发生的事情。 I get the following error (below).我收到以下错误(如下)。 I am new to flutter and any help would be appreciated.我是 flutter 的新手,我们将不胜感激。 What I am trying to do here is that anytime someone double clicks on the chart (using the cross hair functionality), it creates a new data point and re-draws the chart.我在这里尝试做的是,只要有人双击图表(使用十字准线功能),它就会创建一个新数据点并重新绘制图表。 Thank you!谢谢!

Error: ════════ Exception caught by foundation library ════════════════════════════════ The following assertion was thrown while dispatching notifications for PlotData: Build scheduled during frame.错误:════════基础库捕获的异常═══════════════════════════════以下断言在为 PlotData 发送通知时抛出:在帧期间计划构建。 While the widget tree was being built, laid out, and painted, a new frame was scheduled to rebuild the widget tree.在构建、布局和绘制小部件树的同时,安排了一个新框架来重建小部件树。

All my Code:我所有的代码:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
// This tokwidget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => PlotData()),
      ],
      child: Consumer<PlotData>(builder: (context, plotData, _) {
        return MaterialApp(
          home: FirstPage(),
        );
      }),
    );
  }
}

class SalesData {
  SalesData(this.year, this.sales);
  final String year;
  final double sales;
}

class PlotData extends ChangeNotifier {
  List<SalesData> data = [
    SalesData('Jan', 35),
    SalesData('Feb', 28),
    SalesData('Mar', 34),
    SalesData('Apr', 32),
    SalesData('May', 40)
  ];
  void addData(List selectedPoint) {
    print("Current Length of data: " + data.length.toString());
    List<SalesData> addPoint = [
      SalesData(selectedPoint[0], double.parse(selectedPoint[1]))
    ];
    data = data + addPoint;
    print("Now Data has Length of: " + data.length.toString());
    notifyListeners();
  }

  List<SalesData> get allData => data;
}

class VolumeChart extends StatelessWidget {
// Holds the selected points by the user
  List selectedPoint = [];
//VolumeChart(this.data);
  @override
  Widget build(BuildContext context) {
    return SfCartesianChart(
      onCrosshairPositionChanging: (CrosshairRenderArgs args) {
// Crosshair calls this function twice, once for the x,y values respectively
// we capture the full data point string in a list that empties itself when new points are selected
        if (selectedPoint.length == 2) {
          selectedPoint = [];
        }
        selectedPoint.add(args.value); // save the coordinates of the point
// @TODO delete this later, used for prinitng coordinates
        if (selectedPoint.length == 2) {
          Provider.of<PlotData>(context, listen: false).addData(selectedPoint);
//print(selectedPoint.toString());
        }
      },
// Enables the tooltip for all the series in chart
      crosshairBehavior: CrosshairBehavior(
          activationMode: ActivationMode.doubleTap, enable: true),
// Initialize category axis
      primaryXAxis: CategoryAxis(),
      series: <ChartSeries>[
// Initialize line series
        LineSeries<SalesData, String>(
// Enables the tooltip for individual series
            enableTooltip: true,
            dataSource: Provider.of<PlotData>(context, listen: true).allData,
            xValueMapper: (SalesData sales, _) => sales.year,
            yValueMapper: (SalesData sales, _) => sales.sales),
      ],
    );
  }
}

class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        VolumeChart(),
      ],
    );
  }
}

You don't need to call an instance of the provider with a listen: true , because that is the default value.您不需要使用listen: true调用提供者的实例,因为这是默认值。

I will recommend that you avoid using a consumer as the direct descendent of the MaterialApp and do this instead;我会建议您避免使用consumer作为MaterialApp的直接后代,而是这样做;

class MyApp extends StatelessWidget {
// This tokwidget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => PlotData()),
      ],
      child:  MaterialApp(
          home: FirstPage.create(context),

      }),
    );
  }
}

Then implement FirstPage as follows;然后实现FirstPage如下;

class FirstPage extends StatelessWidget {
   final PlotData plotData; 
   FirstPage({@required this.plotData});

  static Widget create(BuildContext context){
       final plotDataInstant = Provider.of<PlotData>(context, listen: false);
       return FirstPage(plotData: plotDataInstant);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        VolumeChart(),
      ],
    );
  }
}

I hope this helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在构建期间调用 setState() 或 markNeedsBuild()。 Flutter 提供者 Package - setState() or markNeedsBuild() called during build. Flutter Provider Package 在 flutter 中使用 path_provider package 时出错 - Error using path_provider package in flutter 颤振)蓝牙提供程序错误=&gt;setState()或markNeedsBuild()在构建期间调用 - flutter) Bluetooth provider error =>setState() or markNeedsBuild() called during build Flutter:提供程序 class 中出现“构建期间调用的 setState() 或 markNeedsBuild()”错误 - Flutter: "setState() or markNeedsBuild() called during build" error in provider class 在构建期间调用 Flutter 提供程序抛出错误 setState() 或 markNeedsBuild() - Flutter provider throwing error setState() or markNeedsBuild() called during build 在构建期间更新值时使用 Flutter Provider 的问题 - Problem using Flutter Provider when updating a value during build 与 Flutter 提供程序包相关的错误 - Error related to Flutter provider package Flutter - 提供者 - 在构建期间调用 setstate 或 markneedsbuild() - Flutter - Provider - setstate or markneedsbuild() called during build Flutter 在构建过程中调用了 Provider setState() 或 markNeedsBuild() - Flutter Provider setState() or markNeedsBuild() called during build Flutter:setState() 或 markNeedsBuild() 在使用 Provider 构建期间调用 - Flutter: setState() or markNeedsBuild() called during build with Provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM