-
|
Considering this example: import 'dart:developer';
import 'package:disco/disco.dart';
import 'package:flutter/material.dart';
import 'package:flutter_solidart/flutter_solidart.dart';
@immutable
class ViewModel {
ViewModel();
static final provider = Provider((_) => ViewModel());
final counter = Signal<int>(0);
}
void main() {
runApp(
MaterialApp(
localizationsDelegates: [DefaultMaterialLocalizations.delegate],
home: ProviderScope(
providers: [ViewModel.provider],
child: Scaffold(
body: Center(
child: Builder(
builder: (context) {
return FilledButton(
onPressed: () => showBottomSheet(
context: context,
builder: (_) => Modal(mainContext: context),
),
child: Text('Test'),
);
},
),
),
),
),
),
);
}
class Modal extends StatelessWidget {
const Modal({super.key, required BuildContext mainContext})
: _mainContext = mainContext;
final BuildContext _mainContext;
@override
Widget build(BuildContext context) {
return ProviderScopePortal(
mainContext: _mainContext,
child: SignalBuilder(
builder: (innerContext, _) {
final vm = ViewModel.provider.of(innerContext);
return Column(children: [Text(vm.counter().toString()), Counter()]);
},
),
);
}
}
@immutable
class CounterViewModel {
CounterViewModel({required this.count});
static final provider = Provider.withArgument(
(_, int count) => CounterViewModel(count: count),
dispose: (viewModel) => viewModel.dispose(),
);
final int count;
final counter = Signal<int>(0);
void dispose() {
log('CounterViewModel disposed');
}
}
class Counter extends StatelessWidget {
const Counter({super.key});
@override
Widget build(BuildContext context) {
return ProviderScope(
providers: [CounterViewModel.provider(5)],
child: SignalBuilder(
builder: (innerContext, _) {
final vm = CounterViewModel.provider.of(innerContext);
return Column(
children: [
Text(vm.counter().toString()),
FilledButton(
onPressed: () => vm.counter.updateValue((value) => value + 1),
child: const Text('Increment'),
),
],
);
},
),
);
}
}
The |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
|
I have similar use cases where a route injects a view model provider. When user pop the page, the dispose was never called too. |
Beta Was this translation helpful? Give feedback.
-
|
The providers should dispose when the ProviderScope (who provided them) is removed from the widget tree, so it could be another bug. |
Beta Was this translation helpful? Give feedback.
-
|
By the way, @JohnCido thanks so much for reporting this and the other two bugs. Please, keep reporting if you find more (in theory this package is not so vast, so there should not be many more, but you never know...). Also, let us know if you notice something important that is lacking in this package :) |
Beta Was this translation helpful? Give feedback.
-
|
You can continue to track the progress here #25 |
Beta Was this translation helpful? Give feedback.
You can continue to track the progress here #25