Is SelectorViewModelWidget<ViewModel, MyOwnDataType> possible? #755
JohnnyRainbow81
started this conversation in
General
Replies: 2 comments 5 replies
-
Hey @JohnnyRainbow81 @saileshbro built the feature. Lets see if he can provide some insights as to why that's not working. |
Beta Was this translation helpful? Give feedback.
0 replies
-
@JohnnyRainbow81 I think you missed bundle = bundle.copyWith(value1: i);
bundle = bundle.copyWith(value2: 1000 - i);
bundle = bundle.copyWith(value3: 500 + i); Since you are not reassigning the Working exampleclass MyViewModel extends BaseViewModel {
Bundle bundle = Bundle(value1: 0, value2: 0, value3: 0);
Future<void> increment() async {
for (int i = 0; i < 100; i++) {
debugPrint(i.toString());
await Future.delayed(const Duration(seconds: 1));
bundle = bundle.copyWith(value1: i);
bundle = bundle.copyWith(value2: 1000 - i);
bundle = bundle.copyWith(value3: 500 + i);
notifyListeners();
}
}
}
class ParentScreen extends StatelessWidget {
const ParentScreen({super.key});
@override
Widget build(BuildContext context) {
return ViewModelBuilder.nonReactive(
viewModelBuilder: () => MyViewModel(),
onModelReady: (model) => model.increment(),
builder: (context, model, child) => Scaffold(
body: Center(child: ChildWidget()),
),
);
}
}
class Bundle {
Bundle({
required this.value1,
required this.value2,
required this.value3,
});
int value1 = 0;
int value2 = 0;
int value3 = 0;
Bundle copyWith({
int? value1,
int? value2,
int? value3,
}) {
return Bundle(
value1: value1 ?? this.value1,
value2: value2 ?? this.value2,
value3: value3 ?? this.value3,
);
}
}
class ChildWidget extends SelectorViewModelWidget<MyViewModel, Bundle> {
@override
Widget build(BuildContext context, Bundle value) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(value.value1.toString()),
Text(value.value2.toString()),
Text(value.value3.toString()),
],
);
}
@override
Bundle selector(MyViewModel model) {
return model.bundle;
}
} You can write
|
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I try to use
SelectorViewModelWidget
with my own data type but it doesn't seem to work, since theSelectorViewModelWidget
won't be updated.With regular data types like
int
I don't have problems.Since I thought that an update of
SelectorViewModelWidget
might require a new object, I switched to acopyWith
-constructor to swap out the whole object and refresh the internal pointer. But still no effect..Beta Was this translation helpful? Give feedback.
All reactions