From d5e072c35b5709752d1d530acb774d6d618bc5e7 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Tue, 1 Dec 2015 22:48:36 -0800 Subject: [PATCH] Fix crash when going back in stocks app. Heroes with the same tag have to have keys that are unique across the entire subtree. Since we can now show both stock lists, this means we have to include the tab in the heroes' keys. Fixes #668. --- examples/stocks/lib/stock_home.dart | 23 ++++++++++--------- examples/stocks/lib/stock_list.dart | 4 +++- examples/stocks/lib/stock_row.dart | 19 +++++++++------ .../lib/src/widgets/hero_controller.dart | 2 ++ packages/flutter/lib/src/widgets/heroes.dart | 14 ++++++++++- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/examples/stocks/lib/stock_home.dart b/examples/stocks/lib/stock_home.dart index bb1171ca4c..e10794e8d8 100644 --- a/examples/stocks/lib/stock_home.dart +++ b/examples/stocks/lib/stock_home.dart @@ -199,8 +199,9 @@ class StockHomeState extends State { )); } - Widget buildStockList(BuildContext context, Iterable stocks) { + Widget _buildStockList(BuildContext context, Iterable stocks, StockHomeTab tab) { return new StockList( + keySalt: tab, stocks: stocks.toList(), onAction: _buyStock, onOpen: (Stock stock, Key arrowKey) { @@ -214,6 +215,13 @@ class StockHomeState extends State { ); } + Widget _buildStockTab(BuildContext context, StockHomeTab tab, List stockSymbols) { + return new Container( + key: new ValueKey(tab), + child: _buildStockList(context, _filterBySearchQuery(_getStockList(stockSymbols)).toList(), tab) + ); + } + static const List portfolioSymbols = const ["AAPL","FIZZ", "FIVE", "FLAT", "ZINC", "ZNGA"]; static GlobalKey searchFieldKey = new GlobalKey(); @@ -259,13 +267,6 @@ class StockHomeState extends State { ); } - Widget buildStockTab(BuildContext context, StockHomeTab tab, List stockSymbols) { - return new Container( - key: new ValueKey(tab), - child: buildStockList(context, _filterBySearchQuery(_getStockList(stockSymbols)).toList()) - ); - } - double _viewWidth = 100.0; void _handleSizeChanged(Size newSize) { setState(() { @@ -282,14 +283,14 @@ class StockHomeState extends State { onSizeChanged: _handleSizeChanged, child: new TabBarView( selection: _tabBarSelection, - items: [StockHomeTab.market, StockHomeTab.portfolio], + items: [StockHomeTab.market, StockHomeTab.portfolio], itemExtent: _viewWidth, itemBuilder: (BuildContext context, StockHomeTab tab, _) { switch (tab) { case StockHomeTab.market: - return buildStockTab(context, tab, config.symbols); + return _buildStockTab(context, tab, config.symbols); case StockHomeTab.portfolio: - return buildStockTab(context, tab, portfolioSymbols); + return _buildStockTab(context, tab, portfolioSymbols); default: assert(false); } diff --git a/examples/stocks/lib/stock_list.dart b/examples/stocks/lib/stock_list.dart index ac91716833..95d1f1a1ff 100644 --- a/examples/stocks/lib/stock_list.dart +++ b/examples/stocks/lib/stock_list.dart @@ -5,8 +5,9 @@ part of stocks; class StockList extends StatelessComponent { - StockList({ Key key, this.stocks, this.onOpen, this.onShow, this.onAction }) : super(key: key); + StockList({ Key key, this.keySalt, this.stocks, this.onOpen, this.onShow, this.onAction }) : super(key: key); + final Object keySalt; final List stocks; final StockRowActionCallback onOpen; final StockRowActionCallback onShow; @@ -18,6 +19,7 @@ class StockList extends StatelessComponent { itemExtent: StockRow.kHeight, itemBuilder: (BuildContext context, Stock stock, int index) { return new StockRow( + keySalt: keySalt, stock: stock, onPressed: onOpen, onDoubleTap: onShow, diff --git a/examples/stocks/lib/stock_row.dart b/examples/stocks/lib/stock_row.dart index bad4d7d025..1d61b2967e 100644 --- a/examples/stocks/lib/stock_row.dart +++ b/examples/stocks/lib/stock_row.dart @@ -7,18 +7,22 @@ part of stocks; enum StockRowPartKind { arrow } class StockRowPartKey extends Key { - const StockRowPartKey(this.stock, this.part) : super.constructor(); + const StockRowPartKey(this.keySalt, this.stock, this.part) : super.constructor(); + final Object keySalt; final Stock stock; final StockRowPartKind part; bool operator ==(dynamic other) { - if (other is! StockRowPartKey) + if (identical(this, other)) + return true; + if (other.runtimeType != runtimeType) return false; final StockRowPartKey typedOther = other; - return stock == typedOther.stock && - part == typedOther.part; + return keySalt == typedOther.keySalt + && stock == typedOther.stock + && part == typedOther.part; } - int get hashCode => 37 * (37 * (373) + identityHashCode(stock)) + identityHashCode(part); - String toString() => '[StockRowPartKey ${stock.symbol}:${part.toString().split(".")[1]})]'; + int get hashCode => 37 * (37 * (37 * (373) + identityHashCode(keySalt)) + identityHashCode(stock)) + identityHashCode(part); + String toString() => '[$runtimeType ${keySalt.toString().split(".")[1]}:${stock.symbol}:${part.toString().split(".")[1]}]'; } typedef void StockRowActionCallback(Stock stock, Key arrowKey); @@ -26,11 +30,12 @@ typedef void StockRowActionCallback(Stock stock, Key arrowKey); class StockRow extends StatelessComponent { StockRow({ Stock stock, + Object keySalt, this.onPressed, this.onDoubleTap, this.onLongPressed }) : this.stock = stock, - _arrowKey = new StockRowPartKey(stock, StockRowPartKind.arrow), + _arrowKey = new StockRowPartKey(keySalt, stock, StockRowPartKind.arrow), super(key: new ObjectKey(stock)); final Stock stock; diff --git a/packages/flutter/lib/src/widgets/hero_controller.dart b/packages/flutter/lib/src/widgets/hero_controller.dart index 467f8532fc..e2d86a0a26 100644 --- a/packages/flutter/lib/src/widgets/hero_controller.dart +++ b/packages/flutter/lib/src/widgets/hero_controller.dart @@ -88,6 +88,8 @@ class HeroController extends NavigatorObserver { } Set _getMostValuableKeys() { + assert(_from != null); + assert(_to != null); Set result = new Set(); if (_from.settings.mostValuableKeys != null) result.addAll(_from.settings.mostValuableKeys); diff --git a/packages/flutter/lib/src/widgets/heroes.dart b/packages/flutter/lib/src/widgets/heroes.dart index 004c69fa86..27b133d467 100644 --- a/packages/flutter/lib/src/widgets/heroes.dart +++ b/packages/flutter/lib/src/widgets/heroes.dart @@ -107,7 +107,19 @@ class Hero extends StatefulComponent { assert(tag != null); Key key = hero.widget.key; final Map tagHeroes = heroes.putIfAbsent(tag, () => {}); - assert(!tagHeroes.containsKey(key)); + assert(() { + if (tagHeroes.containsKey(key)) { + debugPrint('Tag: $tag Key: $key'); + assert(() { + 'There are multiple heroes that share the same key within the same subtree. ' + 'Within each subtree for which heroes are to be animated (typically a PageRoute subtree), ' + 'either each Hero must have a unique tag, or, all the heroes with a particular tag must ' + 'have different keys. The relevant tag and key were dumped above. '; + return false; + }); + } + return true; + }); tagHeroes[key] = hero.state; } element.visitChildren(visitor);