1

I am having a class instance(SampleData data) as field in the state class of my widget(SecondRoute). The class instance holds in memory while inspecting using the memory profiler. Is that necessary to set null for the instance in dispose() of state class to avoid holding that class object in memory?

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late SecondRoute secondRoute;

  @override
  void initState() {
    secondRoute = const SecondRoute();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: ElevatedButton(
            child: const Text('Open route'),
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => secondRoute),
              );
            },
          ),
        ));
  }

  @override
  void dispose() {
    super.dispose();
  }
}

class SecondRoute extends StatefulWidget {
  const SecondRoute({Key? key}) : super(key: key);

  @override
  State<SecondRoute> createState() => _SecondRouteState();
}

class _SecondRouteState extends State<SecondRoute> {

  SampleData? data;
  @override
  void initState() {
    data = SampleData('John', 28);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("Second Page"),
        ),
        body: Center(
          child: Column(children: <Widget>[
            Row(
              children: [Text(data!.name!), Text(data!.age!.toString())],
            ),          
          ]),
        ));
  }

  @override
  void dispose() {
    data = null;
    super.dispose();
  }
}

class SampleData {
  SampleData(this.name, this.age);
  final String? name;
  final double? age;
}

memory profilier image

  • 2
    don't worry about that, Dart has a Garbage collector.... Most use cases don't need to even think about this. Dispose of streams and observables on the dispose method. – pedro pimont Oct 27 '21 at 12:22
  • @pedropimont: Thanks for your update! But in my sample, when I have two pages from one page to I have navigated back to another page. And from that I have returned back to the first page. – Kanagambigai Murugan Oct 28 '21 at 06:33
  • while doing, the second page is not getting disposed and therefore the class instance used in that state class is not set. But if I set class instance as null in dispose method, then the value of that instance is null in the memory snapshot. Can you check my attached sample and let me if know, if I have done anything wrong? [sample](https://www.syncfusion.com/downloads/support/directtrac/general/ze/guidelines_sample-1556214980.zip). – Kanagambigai Murugan Oct 28 '21 at 06:33

0 Answers0