leak_detector 0.1.2+1-bate copy "leak_detector: ^0.1.2+1-bate" to clipboard
leak_detector: ^0.1.2+1-bate copied to clipboard

outdated

This is a plug-in used to detect page memory leaks. You can use it to detect whether there is a memory leak in `State` and `StatefulElement`.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:leak_detector/leak_detector.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  GlobalKey<NavigatorState> _navigatorKey = GlobalKey();

  @override
  void initState() {
    super.initState();
    //must init the tools
    LeakDetector().init(maxRetainingPath: 300);
    LeakDetector().onLeakedStream.listen((LeakedInfo info) {
      showLeakedInfoPage(_navigatorKey.currentContext, info);
    });
    LeakDetector().onEventStream.listen((DetectorEvent event) {
      print(event);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: _navigatorKey,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextButton(
                onPressed: () {
                  Navigator.of(_navigatorKey.currentContext).push(MaterialPageRoute(builder: (_) => TestLeakPage()));
                },
                child: Text(
                  'jump',
                ),
              ),
              TextButton(
                onPressed: () {
                  getLeakedRecording().then((List<LeakedInfo> list) {
                    showLeakedInfoListPage(_navigatorKey.currentContext, list);
                  });
                },
                child: Text(
                  'read history',
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class TestLeakPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return TestLeakPageState();
  }
}

class TestLeakPageState extends State with StateLeakMixin {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('leaked page'),
      ),
      body: Center(
        child: TextButton(
          onPressed: () {
            //the 'context' is leaked
            Navigator.of(context).pop(context);
          },
          child: Text(
            'pop',
          ),
        ),
      ),
    );
  }
}
60
likes
0
pub points
85%
popularity

Publisher

unverified uploader

This is a plug-in used to detect page memory leaks. You can use it to detect whether there is a memory leak in `State` and `StatefulElement`.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, path, sqflite, vm_service

More

Packages that depend on leak_detector