Code Monkey home page Code Monkey logo

Comments (4)

av4625 avatar av4625 commented on May 25, 2024

I have done some further testing and removing checkToShowDot does not fix the memory leak.
But removing my touchCallback does fix the memory leak.

I also tried keeping my callbacks for checkToShowDot and touchCallback but setting handleBuiltInTouches to false, this also doesn't fix the memory leak.

from fl_chart.

av4625 avatar av4625 commented on May 25, 2024

After even more testing:

I found if the callback I provide for touchCallback contains ref which is a WidgetRef for riverpod I get this issue.

I changed the touchCallback to this (I know its dumb code but I just wanted the bare minimum to make the issue happen):

touchCallback: (final FlTouchEvent ev, final LineTouchResponse? res) {
  if (ref != null) {
  }
}

I get the memory leak issue. None of my providers contain any FlSpots so I am very confused at how this is happening.

from fl_chart.

av4625 avatar av4625 commented on May 25, 2024

I have done a memory snap shot and have discovered that it is more than just FlSpots that are growing. First snap shot is on first draw of the charts and second snap shot is after a 5 second hover back and forth:
Screenshot 2024-05-02 at 23 35 53

There are 4 charts with 2 lines each in this example.

from fl_chart.

av4625 avatar av4625 commented on May 25, 2024

Here is a full main.dart to reproduce the issue. It is much simpler with only having one chart:

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:fl_chart/fl_chart.dart';

void main() {
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          body: Column(
            children: [
              Expanded(child: Chart(),),
              Expanded(child: Chart(),),
            ]
          )
        )
      ),
      theme: ThemeData(useMaterial3: true),
    );
  }
}

final testProvider = StateProvider<int>((ref) => 0);

class Chart extends ConsumerWidget {
  Chart({super.key});

  static const int _numberOfLines = 3;
  static const int _numberOfDataPoints = 1500;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final int index = ref.watch(testProvider);

    return LineChart(
      LineChartData(
        lineTouchData: LineTouchData(
          handleBuiltInTouches: false,
          touchSpotThreshold: 20,
          touchCallback: (final FlTouchEvent ev, final LineTouchResponse? res) {
            ref.read(testProvider.notifier).state = res?.lineBarSpots?.first.spotIndex ?? 0;
          },
        ),
        gridData: const FlGridData(show: false),
        borderData: FlBorderData(show: false),
        lineBarsData: _buildLineData(index),
      ),
    );
  }

  List<LineChartBarData> _buildLineData(final int index) {
    final List<LineChartBarData> lineData = <LineChartBarData>[];

    for (int i = 0; i < _numberOfLines; ++i) {
      lineData.add(LineChartBarData(
        showingIndicators: <int>[index],
        dotData: FlDotData(show: false),
        spots: () {
          return List<FlSpot>.generate(
            _numberOfDataPoints,
            (index) {
              return FlSpot(index.toDouble(), index.toDouble());
            }
          );
        }()
      ));
    }

    return lineData;
  }
}

You will see that there are 3 lines with 1500 spots each and when you start this app it starts with 4501 FlSpot objects. Then if you hover this just grows and grows and doesn't stop (other objects grow as well).

Required dependencies:

fl_chart: ^0.67.0
flutter_riverpod: ^2.5.1

from fl_chart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.