From 50b32be9a87e001b434e8835381ae5f371a59980 Mon Sep 17 00:00:00 2001 From: Sergey Molchanovsky Date: Thu, 5 Dec 2024 12:22:58 +0300 Subject: [PATCH 1/7] better withBuildChild line len 80 --- flutter_mobx/lib/src/observer.dart | 28 +++++++++--------------- flutter_mobx/test/flutter_mobx_test.dart | 2 +- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/flutter_mobx/lib/src/observer.dart b/flutter_mobx/lib/src/observer.dart index 7dbe8674..f28d911b 100644 --- a/flutter_mobx/lib/src/observer.dart +++ b/flutter_mobx/lib/src/observer.dart @@ -23,31 +23,24 @@ class Observer extends StatelessObserverWidget { required this.builder, super.name, super.warnWhenNoObservables, - }) : debugConstructingStackFrame = debugFindConstructingStackFrame(), - builderWithChild = null, - child = null, - assert(builder != null); + }) : debugConstructingStackFrame = debugFindConstructingStackFrame(); /// Observer which excludes the child branch from being rebuilt + /// + /// - [builderWithChild] is a builder function with a child widget as a parameter; + /// + /// - [child] is the widget to pass to the [builderWithChild] function. // ignore: prefer_const_constructors_in_immutables Observer.withBuiltChild({ super.key, - required this.builderWithChild, - required this.child, + required Widget Function(BuildContext, Widget) builderWithChild, + required Widget child, super.name, super.warnWhenNoObservables, }) : debugConstructingStackFrame = debugFindConstructingStackFrame(), - builder = null, - assert(builderWithChild != null && child != null); - - /// regular builder, suitable for most cases - final WidgetBuilder? builder; - - /// builder function with child parameter - final TransitionBuilder? builderWithChild; + builder = ((context) => builderWithChild(context, child)); - /// The child widget to pass to the [builderWithChild]. - final Widget? child; + final WidgetBuilder builder; /// The stack frame pointing to the source that constructed this instance. final String? debugConstructingStackFrame; @@ -60,8 +53,7 @@ class Observer extends StatelessObserverWidget { : ''); @override - Widget build(BuildContext context) => - builderWithChild?.call(context, child) ?? builder!.call(context); + Widget build(BuildContext context) => builder.call(context); /// Matches constructor stack frames, in both VM and web environments. static final _constructorStackFramePattern = RegExp(r'\bnew\b'); diff --git a/flutter_mobx/test/flutter_mobx_test.dart b/flutter_mobx/test/flutter_mobx_test.dart index 89eaba3f..cba357d5 100644 --- a/flutter_mobx/test/flutter_mobx_test.dart +++ b/flutter_mobx/test/flutter_mobx_test.dart @@ -76,7 +76,7 @@ void main() { onPressed: () => message.value = 'Clicked', child: Container()), Text(message.value, key: key1), - child!, + child, Builder(builder: (context) { return Text(message.value, key: key3); }), From 386f17fcf919dfd1cb4a8f778b894d3326ecb710 Mon Sep 17 00:00:00 2001 From: Sergey Molchanovsky Date: Thu, 5 Dec 2024 12:48:03 +0300 Subject: [PATCH 2/7] docs --- docs/docs/api/observers.mdx | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/docs/api/observers.mdx b/docs/docs/api/observers.mdx index 5b11ae8a..6d52d49a 100644 --- a/docs/docs/api/observers.mdx +++ b/docs/docs/api/observers.mdx @@ -90,6 +90,35 @@ class _CounterExampleState extends State { } ``` +#### Performance optimizations +Mostly you try to make `Observer` as small as possible. But sometimes you want to exclude +the widget subtree from being rebuilt and improve the performance. + +For that you can use an `Observer.withBuiltChild` constructor. It takes `builderWithChild` +and `child` arguments and internally uses the same technique as in [AnimatedBuilder](https://api.flutter.dev/flutter/widgets/AnimatedBuilder-class.html) + +Here is an example: + +```dart +final obsColor = Observable(Color(0xFF000000)); + +Observer.withBuiltChild( + builderWithChild: (context, child) { + return Container( + padding: const EdgeInsets.all(16), + color: obsColor.value, // this widget will be rebuilt when obsColor changes + child: child, + ); + }, + child: ListView.builder( // this widget will not be rebuilt + itemCount: 1000, + itemBuilder: (context, index) { + return ListTile(title: Text('Item $index')); + }, + ), +) +``` + ## ReactionBuilder widget If you ever ran into a need for running a reaction when a Widget loads, you most From 0f30ad87ff8f77701138b12ddbfec94fd7f70abb Mon Sep 17 00:00:00 2001 From: Sergey Molchanovsky Date: Thu, 5 Dec 2024 12:53:25 +0300 Subject: [PATCH 3/7] rename builderWithChild parameter to builder --- docs/docs/api/observers.mdx | 13 +++++++------ flutter_mobx/lib/src/observer.dart | 8 ++++---- flutter_mobx/test/flutter_mobx_test.dart | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/docs/docs/api/observers.mdx b/docs/docs/api/observers.mdx index 6d52d49a..3f40cf99 100644 --- a/docs/docs/api/observers.mdx +++ b/docs/docs/api/observers.mdx @@ -94,23 +94,24 @@ class _CounterExampleState extends State { Mostly you try to make `Observer` as small as possible. But sometimes you want to exclude the widget subtree from being rebuilt and improve the performance. -For that you can use an `Observer.withBuiltChild` constructor. It takes `builderWithChild` +For that you can use an `Observer.withBuiltChild` constructor. It takes `builder` and `child` arguments and internally uses the same technique as in [AnimatedBuilder](https://api.flutter.dev/flutter/widgets/AnimatedBuilder-class.html) -Here is an example: +Here is a brief example: ```dart -final obsColor = Observable(Color(0xFF000000)); +final obsColor = Observable(Colors.green); Observer.withBuiltChild( - builderWithChild: (context, child) { + builder: (context, child) { return Container( padding: const EdgeInsets.all(16), - color: obsColor.value, // this widget will be rebuilt when obsColor changes + color: obsColor.value, // this widget will be rebuilt when the color value changes child: child, ); }, - child: ListView.builder( // this widget will not be rebuilt + child: ListView.builder( // this part will not be rebuilt + shrinkWrap: true, itemCount: 1000, itemBuilder: (context, index) { return ListTile(title: Text('Item $index')); diff --git a/flutter_mobx/lib/src/observer.dart b/flutter_mobx/lib/src/observer.dart index f28d911b..bf7898dc 100644 --- a/flutter_mobx/lib/src/observer.dart +++ b/flutter_mobx/lib/src/observer.dart @@ -27,18 +27,18 @@ class Observer extends StatelessObserverWidget { /// Observer which excludes the child branch from being rebuilt /// - /// - [builderWithChild] is a builder function with a child widget as a parameter; + /// - [builder] is a builder function with a child widget as a parameter; /// - /// - [child] is the widget to pass to the [builderWithChild] function. + /// - [child] is the widget to pass to the [builder] function. // ignore: prefer_const_constructors_in_immutables Observer.withBuiltChild({ super.key, - required Widget Function(BuildContext, Widget) builderWithChild, + required Widget Function(BuildContext, Widget) builder, required Widget child, super.name, super.warnWhenNoObservables, }) : debugConstructingStackFrame = debugFindConstructingStackFrame(), - builder = ((context) => builderWithChild(context, child)); + builder = ((context) => builder(context, child)); final WidgetBuilder builder; diff --git a/flutter_mobx/test/flutter_mobx_test.dart b/flutter_mobx/test/flutter_mobx_test.dart index cba357d5..90bd79e3 100644 --- a/flutter_mobx/test/flutter_mobx_test.dart +++ b/flutter_mobx/test/flutter_mobx_test.dart @@ -69,7 +69,7 @@ void main() { await tester.pumpWidget( MaterialApp( home: Observer.withBuiltChild( - builderWithChild: (context, child) { + builder: (context, child) { return Column( children: [ ElevatedButton( From dda1b9589e730e630b334225e8a2eff3524b4b50 Mon Sep 17 00:00:00 2001 From: Sergey Molchanovsky Date: Thu, 5 Dec 2024 13:31:59 +0300 Subject: [PATCH 4/7] bump ver. --- flutter_mobx/CHANGELOG.md | 4 ++++ flutter_mobx/lib/version.dart | 2 +- flutter_mobx/pubspec.yaml | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/flutter_mobx/CHANGELOG.md b/flutter_mobx/CHANGELOG.md index 316eb87e..5f8dbad0 100644 --- a/flutter_mobx/CHANGELOG.md +++ b/flutter_mobx/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.2.2 + +- fix: removed runtime asserts and nullables from `Observer.withBuiltChild` - [@subzero911](https://github.com/subzero911) + ## 2.2.1+1 - Analyzer fixes diff --git a/flutter_mobx/lib/version.dart b/flutter_mobx/lib/version.dart index 479df147..ff46c3cb 100644 --- a/flutter_mobx/lib/version.dart +++ b/flutter_mobx/lib/version.dart @@ -1,4 +1,4 @@ // Generated via set_version.dart. !!!DO NOT MODIFY BY HAND!!! /// The current version as per `pubspec.yaml`. -const version = '2.2.1+1'; +const version = '2.2.2'; diff --git a/flutter_mobx/pubspec.yaml b/flutter_mobx/pubspec.yaml index fbfc69b8..7f48feb2 100644 --- a/flutter_mobx/pubspec.yaml +++ b/flutter_mobx/pubspec.yaml @@ -2,7 +2,7 @@ name: flutter_mobx description: Flutter integration for MobX. It provides a set of Observer widgets that automatically rebuild when the tracked observables change. -version: 2.2.1+1 +version: 2.2.2 repository: https://github.com/mobxjs/mobx.dart issue_tracker: https://github.com/mobxjs/mobx.dart/issues From 86c04603e92a8adad798d323e27a6d727226d1bc Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 15 Dec 2024 07:46:53 +0530 Subject: [PATCH 5/7] build(deps): bump nanoid from 3.3.7 to 3.3.8 in /docs (#1031) Bumps [nanoid](https://github.com/ai/nanoid) from 3.3.7 to 3.3.8. - [Release notes](https://github.com/ai/nanoid/releases) - [Changelog](https://github.com/ai/nanoid/blob/main/CHANGELOG.md) - [Commits](https://github.com/ai/nanoid/compare/3.3.7...3.3.8) --- updated-dependencies: - dependency-name: nanoid dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- docs/pnpm-lock.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/pnpm-lock.yaml b/docs/pnpm-lock.yaml index ffb14689..c1dd2a76 100644 --- a/docs/pnpm-lock.yaml +++ b/docs/pnpm-lock.yaml @@ -3483,8 +3483,8 @@ packages: mz@2.7.0: resolution: {integrity: sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==} - nanoid@3.3.7: - resolution: {integrity: sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==} + nanoid@3.3.8: + resolution: {integrity: sha512-WNLf5Sd8oZxOm+TzppcYk8gVOgP+l58xNy58D0nbUnOxOWRWvlcCV4kUF7ltmI6PsrLl/BgKEyS4mqsGChFN0w==} engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} hasBin: true @@ -9615,7 +9615,7 @@ snapshots: object-assign: 4.1.1 thenify-all: 1.6.0 - nanoid@3.3.7: {} + nanoid@3.3.8: {} negotiator@0.6.3: {} @@ -10081,7 +10081,7 @@ snapshots: postcss@8.4.38: dependencies: - nanoid: 3.3.7 + nanoid: 3.3.8 picocolors: 1.0.0 source-map-js: 1.2.0 From 9e0f16e29468ff7f33690ea87c29810de24d11e0 Mon Sep 17 00:00:00 2001 From: Sergey Molchanovsky Date: Mon, 16 Dec 2024 14:40:50 +0300 Subject: [PATCH 6/7] revert: ignore: unused_element --- mobx_codegen/analysis_options.yaml | 2 -- mobx_codegen/test/generator_usage_test.dart | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/mobx_codegen/analysis_options.yaml b/mobx_codegen/analysis_options.yaml index e28d8613..998047e8 100644 --- a/mobx_codegen/analysis_options.yaml +++ b/mobx_codegen/analysis_options.yaml @@ -1,8 +1,6 @@ include: package:lints/recommended.yaml analyzer: - errors: - unused_element: ignore exclude: - test/data/**.dart - test/*.g.dart diff --git a/mobx_codegen/test/generator_usage_test.dart b/mobx_codegen/test/generator_usage_test.dart index 950adeea..833aafc1 100644 --- a/mobx_codegen/test/generator_usage_test.dart +++ b/mobx_codegen/test/generator_usage_test.dart @@ -9,6 +9,7 @@ class TestStore = _TestStore with _$TestStore; bool customEquals(String? oldValue, String? newValue) => oldValue != newValue; abstract class _TestStore with Store { + // ignore: unused_element _TestStore(this.field1, {this.field2}); @observable From 0185ad20a424d3bc1497d7e581330e794dc1b385 Mon Sep 17 00:00:00 2001 From: Sergey Molchanovsky Date: Mon, 16 Dec 2024 14:42:17 +0300 Subject: [PATCH 7/7] bump melos --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index e4a07c00..0e5bff32 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,4 +4,4 @@ environment: sdk: ">=3.0.0 <4.0.0" dev_dependencies: - melos: ^5.2.1 + melos: ^6.2.0