"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/packages/flutter/lib/src/widgets/disposable_build_context.dart" (13 Nov 2020, 2546 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 // @dart = 2.8
    6 
    7 import 'framework.dart';
    8 
    9 /// Provides non-leaking access to a [BuildContext].
   10 ///
   11 /// A [BuildContext] is only valid if it is pointing to an active [Element].
   12 /// Once the [Element] is unmounted, the [BuildContext] should not be accessed
   13 /// further. This class makes it possible for a [StatefulWidget] to share its
   14 /// build context safely with other objects.
   15 ///
   16 /// Creators of this object must guarantee the following:
   17 ///
   18 ///   1. They create this object at or after [State.initState] but before
   19 ///      [State.dispose]. In particular, do not attempt to create this from the
   20 ///      constructor of a state.
   21 ///   2. They call [dispose] from [State.dispose].
   22 ///
   23 /// This object will not hold on to the [State] after disposal.
   24 @optionalTypeArgs
   25 class DisposableBuildContext<T extends State> {
   26   /// Creates an object that provides access to a [BuildContext] without leaking
   27   /// a [State].
   28   ///
   29   /// Creators must call [dispose] when the [State] is disposed.
   30   ///
   31   /// The [State] must not be null, and [State.mounted] must be true.
   32   DisposableBuildContext(this._state)
   33       : assert(_state != null),
   34         assert(_state.mounted, 'A DisposableBuildContext was given a BuildContext for an Element that is not mounted.');
   35 
   36   T _state;
   37 
   38   /// Provides safe access to the build context.
   39   ///
   40   /// If [dispose] has been called, will return null.
   41   ///
   42   /// Otherwise, asserts the [_state] is still mounted and returns its context.
   43   BuildContext get context {
   44     assert(_debugValidate());
   45     if (_state == null) {
   46       return null;
   47     }
   48     return _state.context;
   49   }
   50 
   51   /// Called from asserts or tests to determine whether this object is in a
   52   /// valid state.
   53   ///
   54   /// Always returns true, but will assert if [dispose] has not been called
   55   /// but the state this is tracking is unmounted.
   56   bool _debugValidate() {
   57     assert(
   58       _state == null || _state.mounted,
   59       'A DisposableBuildContext tried to access the BuildContext of a disposed '
   60       'State object. This can happen when the creator of this '
   61       'DisposableBuildContext fails to call dispose when it is disposed.',
   62     );
   63     return true;
   64   }
   65 
   66 
   67   /// Marks the [BuildContext] as disposed.
   68   ///
   69   /// Creators of this object must call [dispose] when their [Element] is
   70   /// unmounted, i.e. when [State.dispose] is called.
   71   void dispose() {
   72     _state = null;
   73   }
   74 }