Aggressively try to catch misuse of DropDownButton

Fixes https://github.com/flutter/flutter/issues/948.
I hope.
This commit is contained in:
Hixie 2015-12-15 16:30:04 -08:00
parent fce3a244d3
commit a87351ad88
3 changed files with 13 additions and 4 deletions

View File

@ -209,7 +209,9 @@ class DropDownButton<T> extends StatefulComponent {
this.value, this.value,
this.onChanged, this.onChanged,
this.elevation: 8 this.elevation: 8
}) : super(key: key); }) : super(key: key) {
assert(items.where((DropDownMenuItem<T> item) => item.value == value).length == 1);
}
final List<DropDownMenuItem<T>> items; final List<DropDownMenuItem<T>> items;
final T value; final T value;
@ -225,6 +227,7 @@ class _DropDownButtonState<T> extends State<DropDownButton<T>> {
void initState() { void initState() {
super.initState(); super.initState();
_updateSelectedIndex(); _updateSelectedIndex();
assert(_selectedIndex != null);
} }
void didUpdateConfig(DropDownButton<T> oldConfig) { void didUpdateConfig(DropDownButton<T> oldConfig) {

View File

@ -433,7 +433,8 @@ class RenderStack extends RenderStackBase {
/// Implements the same layout algorithm as RenderStack but only paints the child /// Implements the same layout algorithm as RenderStack but only paints the child
/// specified by index. /// specified by index.
/// Note: although only one child is displayed, the cost of the layout algorithm is ///
/// Although only one child is displayed, the cost of the layout algorithm is
/// still O(N), like an ordinary stack. /// still O(N), like an ordinary stack.
class RenderIndexedStack extends RenderStackBase { class RenderIndexedStack extends RenderStackBase {
RenderIndexedStack({ RenderIndexedStack({
@ -443,11 +444,14 @@ class RenderIndexedStack extends RenderStackBase {
}) : _index = index, super( }) : _index = index, super(
children: children, children: children,
alignment: alignment alignment: alignment
); ) {
assert(index != null);
}
int get index => _index; int get index => _index;
int _index; int _index;
void set index (int value) { void set index (int value) {
assert(value != null);
if (_index != value) { if (_index != value) {
_index = value; _index = value;
markNeedsLayout(); markNeedsLayout();

View File

@ -962,7 +962,9 @@ class IndexedStack extends MultiChildRenderObjectWidget {
Key key, Key key,
this.alignment: const FractionalOffset(0.0, 0.0), this.alignment: const FractionalOffset(0.0, 0.0),
this.index: 0 this.index: 0
}) : super(key: key, children: children); }) : super(key: key, children: children) {
assert(index != null);
}
/// The index of the child to show. /// The index of the child to show.
final int index; final int index;