204 lines
11 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="API docs for the setState method from the SideBarContentState class, for the Dart programming language.">
<title>setState method - SideBarContentState class - SideBarContent library - Dart API</title>
<!-- required because all the links are pseudo-absolute -->
<base href="../..">
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500,400i,400,300|Source+Sans+Pro:400,300,700" rel="stylesheet">
<link rel="stylesheet" href="static-assets/github.css">
<link rel="stylesheet" href="static-assets/styles.css">
<link rel="icon" href="static-assets/favicon.png">
</head>
<body>
<div id="overlay-under-drawer"></div>
<header id="title">
<button id="sidenav-left-toggle" type="button">&nbsp;</button>
<ol class="breadcrumbs gt-separated dark hidden-xs">
<li><a href="index.html">touch_demonstrator</a></li>
<li><a href="pages_SideBarContent/pages_SideBarContent-library.html">SideBarContent</a></li>
<li><a href="pages_SideBarContent/SideBarContentState-class.html">SideBarContentState</a></li>
<li class="self-crumb">setState method</li>
</ol>
<div class="self-name">setState</div>
<form class="search navbar-right" role="search">
<input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
</form>
</header>
<main>
<div class="col-xs-6 col-sm-3 col-md-2 sidebar sidebar-offcanvas-left">
<h5>SideBarContentState class</h5>
<ol>
<li class="section-title"><a href="pages_SideBarContent/SideBarContentState-class.html#constructors">Constructors</a></li>
<li><a href="pages_SideBarContent/SideBarContentState/SideBarContentState.html">SideBarContentState</a></li>
<li class="section-title inherited">
<a href="pages_SideBarContent/SideBarContentState-class.html#instance-properties">Properties</a>
</li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/context.html">context</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/hashCode.html">hashCode</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/mounted.html">mounted</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/runtimeType.html">runtimeType</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/widget.html">widget</a></li>
<li class="section-title"><a href="pages_SideBarContent/SideBarContentState-class.html#instance-methods">Methods</a></li>
<li><a href="pages_SideBarContent/SideBarContentState/build.html">build</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/deactivate.html">deactivate</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/debugFillProperties.html">debugFillProperties</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/didChangeDependencies.html">didChangeDependencies</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/didUpdateWidget.html">didUpdateWidget</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/dispose.html">dispose</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/initState.html">initState</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/noSuchMethod.html">noSuchMethod</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/reassemble.html">reassemble</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/setState.html">setState</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/toDiagnosticsNode.html">toDiagnosticsNode</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/toString.html">toString</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/toStringShort.html">toStringShort</a></li>
<li class="section-title inherited"><a href="pages_SideBarContent/SideBarContentState-class.html#operators">Operators</a></li>
<li class="inherited"><a href="pages_SideBarContent/SideBarContentState/operator_equals.html">operator ==</a></li>
</ol>
</div><!--/.sidebar-offcanvas-->
<div class="col-xs-12 col-sm-9 col-md-8 main-content">
<h1>setState method</h1>
<section class="multi-line-signature">
<div>
<ol class="annotation-list">
<li>@protected</li>
</ol>
</div>
<span class="returntype">void</span>
<span class="name ">setState</span>
(<wbr><span class="parameter" id="setState-param-fn"><span class="type-annotation">VoidCallback</span> <span class="parameter-name">fn</span></span>)
<div class="features">@protected, inherited</div>
</section>
<section class="desc markdown">
<p>Notify the framework that the internal state of this object has changed.</p>
<p>Whenever you change the internal state of a <code>State</code> object, make the
change in a function that you pass to <a href="pages_SideBarContent/SideBarContentState/setState.html">setState</a>:</p>
<pre class="language-dart"><code class="language-dart">setState(() { _myState = newValue });
</code></pre>
<p>The provided callback is immediately called synchronously. It must not
return a future (the callback cannot be <code>async</code>), since then it would be
unclear when the state was actually being set.</p>
<p>Calling <a href="pages_SideBarContent/SideBarContentState/setState.html">setState</a> notifies the framework that the internal state of this
object has changed in a way that might impact the user interface in this
subtree, which causes the framework to schedule a <a href="pages_SideBarContent/SideBarContentState/build.html">build</a> for this <code>State</code>
object.</p>
<p>If you just change the state directly without calling <a href="pages_SideBarContent/SideBarContentState/setState.html">setState</a>, the
framework might not schedule a <a href="pages_SideBarContent/SideBarContentState/build.html">build</a> and the user interface for this
subtree might not be updated to reflect the new state.</p>
<p>Generally it is recommended that the <code>setState</code> method only be used to
wrap the actual changes to the state, not any computation that might be
associated with the change. For example, here a value used by the <a href="pages_SideBarContent/SideBarContentState/build.html">build</a>
function is incremented, and then the change is written to disk, but only
the increment is wrapped in the <code>setState</code>:</p>
<pre class="language-dart"><code class="language-dart">Future&lt;void&gt; _incrementCounter() async {
setState(() {
_counter++;
});
Directory directory = await getApplicationDocumentsDirectory();
final String dirName = directory.path;
await File('$dir/counter.txt').writeAsString('$_counter');
}
</code></pre>
<p>It is an error to call this method after the framework calls <a href="pages_SideBarContent/SideBarContentState/dispose.html">dispose</a>.
You can determine whether it is legal to call this method by checking
whether the <a href="pages_SideBarContent/SideBarContentState/mounted.html">mounted</a> property is true.</p>
</section>
<section class="summary source-code" id="source">
<h2><span>Implementation</span> </h2>
<pre class="language-dart"><code class="language-dart">@protected
void setState(VoidCallback fn) {
assert(fn != null);
assert(() {
if (_debugLifecycleState == _StateLifecycle.defunct) {
throw FlutterError(
&#39;setState() called after dispose(): $this\n&#39;
&#39;This error happens if you call setState() on a State object for a widget that &#39;
&#39;no longer appears in the widget tree (e.g., whose parent widget no longer &#39;
&#39;includes the widget in its build). This error can occur when code calls &#39;
&#39;setState() from a timer or an animation callback. The preferred solution is &#39;
&#39;to cancel the timer or stop listening to the animation in the dispose() &#39;
&#39;callback. Another solution is to check the &quot;mounted&quot; property of this &#39;
&#39;object before calling setState() to ensure the object is still in the &#39;
&#39;tree.\n&#39;
&#39;This error might indicate a memory leak if setState() is being called &#39;
&#39;because another object is retaining a reference to this State object &#39;
&#39;after it has been removed from the tree. To avoid memory leaks, &#39;
&#39;consider breaking the reference to this object during dispose().&#39;
);
}
if (_debugLifecycleState == _StateLifecycle.created &amp;&amp; !mounted) {
throw FlutterError(
&#39;setState() called in constructor: $this\n&#39;
&#39;This happens when you call setState() on a State object for a widget that &#39;
&#39;hasn\&#39;t been inserted into the widget tree yet. It is not necessary to call &#39;
&#39;setState() in the constructor, since the state is already assumed to be dirty &#39;
&#39;when it is initially created.&#39;
);
}
return true;
}());
final dynamic result = fn() as dynamic;
assert(() {
if (result is Future) {
throw FlutterError(
&#39;setState() callback argument returned a Future.\n&#39;
&#39;The setState() method on $this was called with a closure or method that &#39;
&#39;returned a Future. Maybe it is marked as &quot;async&quot;.\n&#39;
&#39;Instead of performing asynchronous work inside a call to setState(), first &#39;
&#39;execute the work (without updating the widget state), and then synchronously &#39;
&#39;update the state inside a call to setState().&#39;
);
}
&#47;&#47; We ignore other types of return values so that you can do things like:
&#47;&#47; setState(() =&gt; x = 3);
return true;
}());
_element.markNeedsBuild();
}</code></pre>
</section>
</div> <!-- /.main-content -->
<div class="col-xs-6 col-sm-6 col-md-2 sidebar sidebar-offcanvas-right">
</div><!--/.sidebar-offcanvas-->
</main>
<footer>
<span class="no-break">
touch_demonstrator 1.0.0+1
</span>
</footer>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="static-assets/typeahead.bundle.min.js"></script>
<script src="static-assets/highlight.pack.js"></script>
<script src="static-assets/URI.js"></script>
<script src="static-assets/script.js"></script>
</body>
</html>