df_will_dispose 0.3.3 copy "df_will_dispose: ^0.3.3" to clipboard
df_will_dispose: ^0.3.3 copied to clipboard

discontinuedreplaced by: df_cleanup

A lightweight package to mark resources for disposal upon definition, simplifying your code.

example/example.dart

//.title
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//
// Dart/Flutter (DF) Packages by DevCetra.com & contributors. Use of this
// source code is governed by an an MIT-style license that can be found in the
// LICENSE file located in this project's root directory.
//
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//.title~

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

// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

class Example1 extends StatefulWidget {
  const Example1({super.key});

  @override
  _Example1State createState() => _Example1State();
}

// Option 1: WillDisposeState<MyWidget>.
// Option 2: State<MyWidget> with DisposeMixin, WillDisposeMixin.
class _Example1State extends WillDisposeState<Example1> {
  // Define resources and schedule them to be disposed when this widget gets
  // removed from the widget tree.
  late final _textController = willDispose(TextEditingController());
  late final _valueNotifier = willDispose(ValueNotifier('Initial Value'));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('WillDispose Example')),
      body: Column(
        children: [
          TextField(controller: _textController),
          ValueListenableBuilder<String>(
            valueListenable: _valueNotifier,
            builder: (context, value, child) => Text('Value: $value'),
          ),
          ElevatedButton(
            onPressed: () {
              _valueNotifier.value = 'Updated Value';
            },
            child: const Text('Update Value'),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    // Resources marked with `willDispose` will be disposed automatically here.
    super.dispose();
  }
}

// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

class Example2 extends WillDisposeWidget {
  const Example2({super.key});

  @override
  Widget build(BuildContext context, WillDispose willDispose) {
    // Define resources and schedule them to be disposed when this widget gets
    // removed from the widget tree.
    final textController = willDispose(TextEditingController());
    final focusNode = willDispose(FocusNode());

    return Column(
      children: [
        TextField(
          controller: textController,
          focusNode: focusNode,
        ),
      ],
    );
  }
}

void main() => runApp(const MaterialApp(home: Example1()));
3
likes
0
pub points
48%
popularity

Publisher

verified publisherdevcetra.com

A lightweight package to mark resources for disposal upon definition, simplifying your code.

Repository (GitHub)
View/report issues

Topics

#dispose #hooks #state #state-management #widget

Funding

Consider supporting this project:

www.buymeacoffee.com

License

unknown (license)

Dependencies

flutter

More

Packages that depend on df_will_dispose