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
2eee5ae9
Unverified
Commit
2eee5ae9
authored
Dec 16, 2020
by
Shi-Hao Hong
Committed by
GitHub
Dec 16, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[State Restoration] RestorableBoolN (#71653)
parent
824c8cd0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
25 deletions
+58
-25
restoration_properties.dart
packages/flutter/lib/src/widgets/restoration_properties.dart
+50
-24
restorable_property_test.dart
packages/flutter/test/widgets/restorable_property_test.dart
+8
-1
No files found.
packages/flutter/lib/src/widgets/restoration_properties.dart
View file @
2eee5ae9
...
...
@@ -143,19 +143,11 @@ abstract class RestorableValue<T> extends RestorableProperty<T> {
void
didUpdateValue
(
T
?
oldValue
);
}
// _RestorablePrimitiveValue and its subclasses do not allow null values in
// anticipation of NNBD (non-nullability by default).
//
// If necessary, we can in the future define a new subclass hierarchy that
// does allow null values for primitive types. Borrowing from lisp where
// functions that returned a bool ended in 'p', a suggested naming scheme for
// these new subclasses could be to add 'N' (for nullable) to the end of a
// class name (e.g. RestorableIntN, RestorableStringN, etc.) to distinguish them
// from their non-nullable friends.
class
_RestorablePrimitiveValue
<
T
extends
Object
>
extends
RestorableValue
<
T
>
{
_RestorablePrimitiveValue
(
this
.
_defaultValue
)
:
assert
(
_defaultValue
!=
null
),
assert
(
debugIsSerializableForRestoration
(
_defaultValue
)),
// _RestorablePrimitiveValueN and its subclasses allows for null values.
// See [_RestorablePrimitiveValue] for the non-nullable version of this class.
class
_RestorablePrimitiveValueN
<
T
extends
Object
?>
extends
RestorableValue
<
T
>
{
_RestorablePrimitiveValueN
(
this
.
_defaultValue
)
:
assert
(
debugIsSerializableForRestoration
(
_defaultValue
)),
super
();
final
T
_defaultValue
;
...
...
@@ -163,28 +155,43 @@ class _RestorablePrimitiveValue<T extends Object> extends RestorableValue<T> {
@override
T
createDefaultValue
()
=>
_defaultValue
;
@override
set
value
(
T
value
)
{
assert
(
value
!=
null
);
super
.
value
=
value
;
}
@override
void
didUpdateValue
(
T
?
oldValue
)
{
assert
(
debugIsSerializableForRestoration
(
value
));
notifyListeners
();
}
@override
T
fromPrimitives
(
Object
?
serialized
)
=>
serialized
as
T
;
@override
Object
?
toPrimitives
()
=>
value
;
}
// _RestorablePrimitiveValue and its subclasses are non-nullable.
// See [_RestorablePrimitiveValueN] for the nullable version of this class.
class
_RestorablePrimitiveValue
<
T
extends
Object
>
extends
_RestorablePrimitiveValueN
<
T
>
{
_RestorablePrimitiveValue
(
T
_defaultValue
)
:
assert
(
_defaultValue
!=
null
),
assert
(
debugIsSerializableForRestoration
(
_defaultValue
)),
super
(
_defaultValue
);
@override
set
value
(
T
value
)
{
assert
(
value
!=
null
);
super
.
value
=
value
;
}
@override
T
fromPrimitives
(
Object
?
serialized
)
{
assert
(
serialized
!=
null
);
return
s
erialized
!
as
T
;
return
s
uper
.
fromPrimitives
(
serialized
)
;
}
@override
Object
toPrimitives
()
{
assert
(
value
!=
null
);
return
value
;
return
super
.
toPrimitives
()!
;
}
}
...
...
@@ -249,9 +256,28 @@ class RestorableBool extends _RestorablePrimitiveValue<bool> {
/// Creates a [RestorableBool].
///
/// {@macro flutter.widgets.RestorableNum.constructor}
///
/// See also:
///
/// * [RestorableBoolN] for the nullable version of this class.
RestorableBool
(
bool
defaultValue
)
:
assert
(
defaultValue
!=
null
),
super
(
defaultValue
);
}
/// A [RestorableProperty] that knows how to store and restore a [bool] that is
/// nullable.
///
/// {@macro flutter.widgets.RestorableNum}
class
RestorableBoolN
extends
_RestorablePrimitiveValueN
<
bool
?>
{
/// Creates a [RestorableBoolN].
///
/// {@macro flutter.widgets.RestorableNum.constructor}
///
/// See also:
///
/// * [RestorableBool] for the non-nullable version of this class.
RestorableBoolN
(
bool
?
defaultValue
)
:
super
(
defaultValue
);
}
/// A base class for creating a [RestorableProperty] that stores and restores a
/// [Listenable].
///
...
...
@@ -313,17 +339,17 @@ abstract class RestorableListenable<T extends Listenable> extends RestorableProp
abstract
class
RestorableChangeNotifier
<
T
extends
ChangeNotifier
>
extends
RestorableListenable
<
T
>
{
@override
void
initWithValue
(
T
value
)
{
_diposeOldValue
();
_di
s
poseOldValue
();
super
.
initWithValue
(
value
);
}
@override
void
dispose
()
{
_diposeOldValue
();
_di
s
poseOldValue
();
super
.
dispose
();
}
void
_diposeOldValue
()
{
void
_di
s
poseOldValue
()
{
if
(
_value
!=
null
)
{
// Scheduling a microtask for dispose to give other entities a chance
// to remove their listeners first.
...
...
packages/flutter/test/widgets/restorable_property_test.dart
View file @
2eee5ae9
...
...
@@ -13,6 +13,7 @@ void main() {
expect
(()
=>
RestorableInt
(
1
).
value
,
throwsAssertionError
);
expect
(()
=>
RestorableString
(
'hello'
).
value
,
throwsAssertionError
);
expect
(()
=>
RestorableBool
(
true
).
value
,
throwsAssertionError
);
expect
(()
=>
RestorableBoolN
(
true
).
value
,
throwsAssertionError
);
expect
(()
=>
RestorableTextEditingController
().
value
,
throwsAssertionError
);
expect
(()
=>
_TestRestorableValue
().
value
,
throwsAssertionError
);
});
...
...
@@ -125,6 +126,7 @@ void main() {
state
.
intValue
.
value
=
10
;
state
.
stringValue
.
value
=
'guten tag'
;
state
.
boolValue
.
value
=
true
;
state
.
nullableBoolValue
.
value
=
false
;
state
.
controllerValue
.
value
.
text
=
'blabla'
;
state
.
objectValue
.
value
=
53
;
});
...
...
@@ -140,6 +142,7 @@ void main() {
state
.
intValue
.
value
=
20
;
state
.
stringValue
.
value
=
'ciao'
;
state
.
boolValue
.
value
=
false
;
state
.
nullableBoolValue
.
value
=
null
;
state
.
controllerValue
.
value
.
text
=
'blub'
;
state
.
objectValue
.
value
=
20
;
});
...
...
@@ -154,6 +157,7 @@ void main() {
expect
(
state
.
intValue
.
value
,
10
);
expect
(
state
.
stringValue
.
value
,
'guten tag'
);
expect
(
state
.
boolValue
.
value
,
true
);
expect
(
state
.
nullableBoolValue
.
value
,
false
);
expect
(
state
.
controllerValue
.
value
.
text
,
'blabla'
);
expect
(
state
.
objectValue
.
value
,
53
);
expect
(
find
.
text
(
'guten tag'
),
findsOneWidget
);
...
...
@@ -166,6 +170,7 @@ void main() {
expect
(
state
.
intValue
.
value
,
42
);
expect
(
state
.
stringValue
.
value
,
'hello world'
);
expect
(
state
.
boolValue
.
value
,
false
);
expect
(
state
.
nullableBoolValue
.
value
,
null
);
expect
(
state
.
controllerValue
.
value
.
text
,
'FooBar'
);
expect
(
state
.
objectValue
.
value
,
55
);
expect
(
find
.
text
(
'hello world'
),
findsOneWidget
);
...
...
@@ -323,6 +328,7 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi
final
RestorableInt
intValue
=
RestorableInt
(
42
);
final
RestorableString
stringValue
=
RestorableString
(
'hello world'
);
final
RestorableBool
boolValue
=
RestorableBool
(
false
);
final
RestorableBoolN
nullableBoolValue
=
RestorableBoolN
(
null
);
final
RestorableTextEditingController
controllerValue
=
RestorableTextEditingController
(
text:
'FooBar'
);
final
_TestRestorableValue
objectValue
=
_TestRestorableValue
();
...
...
@@ -332,7 +338,8 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi
registerForRestoration
(
doubleValue
,
'double'
);
registerForRestoration
(
intValue
,
'int'
);
registerForRestoration
(
stringValue
,
'string'
);
registerForRestoration
(
boolValue
,
'bool'
);
registerForRestoration
(
boolValue
,
'bool'
);
registerForRestoration
(
nullableBoolValue
,
'nullableBool'
);
registerForRestoration
(
controllerValue
,
'controller'
);
registerForRestoration
(
objectValue
,
'object'
);
}
...
...
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