-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leaks after adding touchCallback
and checkToShowDot
#1644
Comments
I have done some further testing and removing I also tried keeping my callbacks for |
After even more testing: I found if the callback I provide for I changed the touchCallback: (final FlTouchEvent ev, final LineTouchResponse? res) {
if (ref != null) {
}
} I get the memory leak issue. None of my providers contain any |
Here is a full 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 Required dependencies:
|
Don't make a duplicate issue.
I'm not sure if its the same as this issue as it only happened after I used the callbacks in the title and the example in that issue doesn't use them: #1106
If it is the same I can close this and record it over there.
Describe the bug
I have multiple charts that all show relating data. The data in this example has around 1500 points per line and I have tested up to 5 lines, so is fairly large. When I hover over one I wanted to show "spots" on the others.
To do this I added a
touchCallback
that saves the hovered spot locations in a riverpodNotifierProvider
. All charts watch this and then in theircheckToShowDot
it checks for the spots that relate to the hover data in theNotifierProvider
and then draws them.I have noticed that the flutter app started using lots of RAM after I added this hover functionality. I have seen over 2Gb and climbing in apples activity monitor. The app starts between 200-300mb. I opened the memory page of devtools and it shows that there where millions of FlSpot objects and they climb in numbers fast when hovering on a chart. It suffers from frame jank and gets slower and slower the more you hover until it becomes unusable.
It is possible I'm misusing the API as I'm new to Dart/Flutter.
To Reproduce
This is one of the 4 charts in the video (the middle one of the 3 on the right side). All 4 charts are almost identical to this.
These two functions are in a parent class:
Before adding the two callbacks I mention above I just had this and the FlSpots never change from
27093
for 5 lines:Screenshots
Video of the charts and how they work (you can see there is major frame jank at times):
https://github.com/imaNNeo/fl_chart/assets/19472253/e42fbd6f-311e-435f-9c4f-a7cd1b5db3b0
A snap of the number of FlSpot objects:
Hovered for about 10 seconds and took a snap of the number of FlSpot objects again:
Versions
The text was updated successfully, but these errors were encountered: