Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
e7cd5d38
Unverified
Commit
e7cd5d38
authored
Apr 20, 2018
by
Jonah Williams
Committed by
GitHub
Apr 20, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Throw FlutterError when calling setState in State constructor (#16759)
* log error when calling setState in constructor
parent
034a663d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
0 deletions
+43
-0
framework.dart
packages/flutter/lib/src/widgets/framework.dart
+9
-0
set_state_5_test.dart
packages/flutter/test/widgets/set_state_5_test.dart
+34
-0
No files found.
packages/flutter/lib/src/widgets/framework.dart
View file @
e7cd5d38
...
...
@@ -1103,6 +1103,15 @@ abstract class State<T extends StatefulWidget> extends Diagnosticable {
'consider breaking the reference to this object during dispose().'
);
}
if
(
_debugLifecycleState
==
_StateLifecycle
.
created
&&
!
mounted
)
{
throw
new
FlutterError
(
'setState() called in constructor:
$this
\n
'
'This happens when you call setState() on a State object for a widget that '
'hasn
\'
t been inserted into the widget tree yet. It is not necessary to call '
'setState() in the constructor, since the state is already assumed to be dirty '
'when it is initially created.'
);
}
return
true
;
}());
final
dynamic
result
=
fn
()
as
dynamic
;
...
...
packages/flutter/test/widgets/set_state_5_test.dart
0 → 100644
View file @
e7cd5d38
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'package:flutter_test/flutter_test.dart'
;
import
'package:flutter/widgets.dart'
;
class
BadWidget
extends
StatefulWidget
{
@override
State
<
StatefulWidget
>
createState
()
=>
new
BadWidgetState
();
}
class
BadWidgetState
extends
State
<
BadWidget
>
{
BadWidgetState
()
{
setState
(()
{
_count
=
1
;
});
}
int
_count
=
0
;
@override
Widget
build
(
BuildContext
context
)
{
return
new
Text
(
_count
.
toString
());
}
}
void
main
(
)
{
testWidgets
(
'setState() catches being used inside a constructor'
,
(
WidgetTester
tester
)
async
{
await
tester
.
pumpWidget
(
new
BadWidget
());
expect
(
tester
.
takeException
(),
const
isInstanceOf
<
FlutterError
>());
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment