mirror of https://github.com/flutter/samples.git
docs: clarify HomeViewModel lifecycle in Compass sample (#2838)
Adds a comment explaining why `HomeViewModel` is not disposed in the Compass sample. This clarifies that the current implementation is safe because the ViewModel does not manage disposable resources. It also highlights that in real applications, proper lifecycle management is required when working with streams, controllers, or subscriptions. Fixes https://github.com/flutter/samples/issues/2788 ## Pre-launch Checklist * [x] I read the [[Flutter Style Guide](https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md)] recently, and have followed its advice. * [x] I signed the [[CLA](https://cla.developers.google.com/)]. * [ ] I have added sample code updates to the [[changelog](https://chatgpt.com/c/CHANGELOG.md)]. * [x] I updated/added relevant documentation (doc comments with `///`). <!-- Links --> [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [CLA]: https://cla.developers.google.com/ [changelog]: ./CHANGELOG.md --------- Co-authored-by: Eric Windmill <eric@ericwindmill.com>pull/2866/head
parent
f755c81579
commit
8bfffd4a84
@ -0,0 +1,39 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../../data/repositories/booking/booking_repository.dart';
|
||||
import '../../../data/repositories/user/user_repository.dart';
|
||||
import '../view_models/home_viewmodel.dart';
|
||||
import 'home_screen.dart';
|
||||
|
||||
class HomeScreenContainer extends StatefulWidget {
|
||||
const HomeScreenContainer({super.key});
|
||||
|
||||
@override
|
||||
State<HomeScreenContainer> createState() => _HomeScreenContainerState();
|
||||
}
|
||||
|
||||
class _HomeScreenContainerState extends State<HomeScreenContainer> {
|
||||
late final HomeViewModel _viewModel;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_viewModel = HomeViewModel(
|
||||
bookingRepository: context.read<BookingRepository>(),
|
||||
userRepository: context.read<UserRepository>(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return HomeScreen(viewModel: _viewModel);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_viewModel.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue