Skip to content

Instantly share code, notes, and snippets.

@Diaga
Last active November 18, 2022 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Diaga/a31751b8fbc9600f31e5c415d06d47ff to your computer and use it in GitHub Desktop.
Save Diaga/a31751b8fbc9600f31e5c415d06d47ff to your computer and use it in GitHub Desktop.
dep map
void main() {
}
// Do not have to define separate depController fields
class TableWidget extends ConsumerWidget {
TableWidget(String arg) : super(depMap: {
'textController': Dep(() {return TextController(arg); }),
'scrollController': Dep(() {return ScrollController(); }),
'tableController': Dep(() {return TableController(); })
});
TextController get textController {
return depContainer.textController;
}
ScrollController get scrollController {
return depContainer.scrollController;
}
TableController get tableController {
return depContainer.tableController;
}
}
class ConsumerWidget {
ConsumerWidget({Map? depMap}) :
depContainer = ConsumerDepContainer(depMap);
late final dynamic depContainer;
}
class ConsumerDepContainer {
late Map<String, Dep> _data;
ConsumerDepContainer(Map? data) {
_data = {};
data?.forEach((k, v) => _data[Symbol(k).toString()] = v);
}
@override
dynamic noSuchMethod(Invocation inv) {
if (inv.isGetter) {
final res = _data[inv.memberName.toString()];
if (res != null) {
return res();
} else {
super.noSuchMethod(inv);
}
}
if (inv.isSetter) {
_data[inv.memberName.toString().replaceAll('=', '')] = inv.positionalArguments.first;
} else {
super.noSuchMethod(inv);
}
}
}
class Dep {
Dep(Function ftn) {}
call() {}
}
// Ignore
class ScrollController {}
class TextController {
TextController(String? arg);
}
class TableController {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment