flutter_in_action_2nd icon indicating copy to clipboard operation
flutter_in_action_2nd copied to clipboard

7.2.2 缺少一种case

Open lwy121810 opened this issue 2 years ago • 0 comments

注意,如果将上面示例中ShareDataWidget.of()方法实现改成调用getElementForInheritedWidgetOfExactType(),运行示例后,点击"Increment"按钮,会发现__TestWidgetState的didChangeDependencies()方法确实不会再被调用,但是其build()仍然会被调用

少写一种case:当_TestWidget被const修饰时,点击按钮其build方法并不会调用

 child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const _TestWidget(),
                TextButton(
                  onPressed: () {
                    setState(() {
                      ++count;
                    });
                  },
                  child: const Text('increment'),
                ),
              ],
            ),

我这里使用了空安全特性,dart会推荐 使用const 修饰构造器方法,在初始化的时候也会推荐使用const,不然会有会有波浪号提示(强迫症看不惯😂)

lwy121810 avatar Jul 08 '22 06:07 lwy121810