Skip to content

Commit 1df89d0

Browse files
committed
remove ctrl params if there is no ctrl
1 parent 942fd75 commit 1df89d0

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Imagine that you can do this :
1212
Instead of this:
1313
```dart
1414
final size = MediaQuery.of(context).size;
15-
final widget = StreamBuilder(
15+
final __widget = StreamBuilder(
1616
initialData: ctrl.textVisible.value,
1717
stream: ctrl.textVisible,
1818
builder: (BuildContext context, snapshot) {
@@ -36,7 +36,7 @@ Instead of this:
3636
}
3737
}
3838
);
39-
return widget;
39+
return __widget;
4040
```
4141
Which is about 20 lines of code, and if you just updated the `:text` property to use a stream variable `:text="ctrl.myTextStream | stream"` that will add another 4 lines of code for the StreamBuilder.
4242

src/generators/class-generator.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ export class ClassCodeGenerator {
8585
@override
8686
Widget build(BuildContext context) {
8787
final _pipeProvider = Provider.of<PipeProvider>(context);
88-
final widget = ${rootChildCode};
89-
return widget;
88+
final __widget = ${rootChildCode};
89+
return __widget;
9090
}`;
9191

9292

@@ -206,7 +206,7 @@ class ${widgetName} extends StatelessWidget${mixinsCode} {
206206
];
207207
const stateVarsInit: string[] = [
208208
...(hasController ? [`ctrl = new ${rootWidget.controller}();`] : []),
209-
...rootWidget.params.filter(a => !!a.name).map(a => `ctrl._${a.name} = widget.${a.name};`),
209+
...(hasController ? rootWidget.params.filter(a => !!a.name).map(a => `ctrl._${a.name} = widget.${a.name};`) : []),
210210
...controllers.filter(a => !a.isPrivate && !a.skipGenerate).map(a => `${hasController ? `ctrl._${a.name} = `: ''}${a.name} = ${a.value ? a.value : `new ${a.type}()`};`),
211211
...rootWidget.vars.map(a => `${hasController ? `ctrl._${a.name} = `: ''}${a.name} = ${a.value};`),
212212
...(hasController ? [`WidgetsBinding.instance.addPostFrameCallback((_) => ctrl.afterFirstBuild(context));`] : [])
@@ -235,21 +235,20 @@ class _${widgetName}State extends State<${widgetName}>${mixinsCode} {
235235
236236
@override
237237
void initState() {
238-
super.initState();
239-
${stateVarsInit.join(`\n `)}
238+
super.initState();${(stateVarsInit.length > 0 ? '\n' : '') + stateVarsInit.join(`\n `)}
240239
}
241240
242241
@override
243242
void didChangeDependencies() {
244-
super.didChangeDependencies();${routeAware ? `\n _routeObserver = Provider.of<RouteObserver<Route>>(context)..subscribe(this, ModalRoute.of(context));` : ''}
245-
${rootWidget.providers.map(a => `${hasController ? `ctrl._${a.name} = `: ''}${a.name} = Provider.of<${a.type}>(context);`).join('\n ')}
246-
${hasController ? `ctrl._load(context);` : ''}
243+
super.didChangeDependencies();${routeAware ? `\n _routeObserver = Provider.of<RouteObserver<Route>>(context)..subscribe(this, ModalRoute.of(context));` : ''
244+
}${rootWidget.providers.map(a => `${hasController ? `ctrl._${a.name} = `: ''}${a.name} = Provider.of<${a.type}>(context);`).join('\n ')
245+
}${hasController ? `ctrl._load(context);` : ''}
247246
}
248247
249248
@override
250-
void dispose() {
251-
${hasController ? `ctrl.dispose();` : ''}${routeAware ? `\n _routeObserver.unsubscribe(this);` : ''}
252-
${controllers.filter(a => a.isPrivate).map(a => `${a.name}.dispose();`).join('\n ')}
249+
void dispose() {${hasController ? `\nctrl.dispose();` : ''
250+
}${routeAware ? `\n _routeObserver.unsubscribe(this);` : ''
251+
}${controllers.filter(a => a.isPrivate).map(a => `${a.name}.dispose();`).join('\n ')}
253252
super.dispose();
254253
}
255254
${buildMethodContent}

0 commit comments

Comments
 (0)