Closed Berthelmaster closed 3 years ago
@Berthelmaster Hi, there. I have the same issue about quit the page but the viewmodel didn't dispose. Could you please tell me how to solve it?
Hi! I switched package. Wasn't able to resolve
Thank you for reply~
Describe the bug When clicking back on emulators, and the view is disposed, the state of the viewmodel is maintained. This means that the next time the view is opened, it is loaded with state from an old view, which is not intended in my app at least.
To Reproduce
main.dart
`void main() { runApp(const MyApp()); }
class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key);
@override Widget build(BuildContext context) { return MultiProvider(providers: [ ChangeNotifierProvider( create: () => CreateRoomViewModel(), ), ChangeNotifierProvider( create: () => RoomSelectionViewModel(), ), ChangeNotifierProvider( create: (context) => RoomInstanceViewModel(), ) ], child: MaterialApp( theme: ThemeData( primarySwatch: Colors.blue ), initialRoute: '/', routes: { / List of routes and what they do / // Find or Create Room '/': (context) => const Main(), // Shows the room and valid courts '/room': (context) => RoomInstance.optional(), // Create a room '/create': (context) => const CreateRoom() }, )); } }`
View Model
`import 'dart:io'; import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:racket_match/services/hub_clients/on_connection_join.dart'; import 'package:racket_match/services/room_service.dart'; import 'package:racket_match/models/match.dart';
class RoomInstanceViewModel with ChangeNotifier {
bool _loading = false; String? _roomId; List _matches = [];
var _onConnectionJoinHub;
RoomInstanceViewModel(){ print('CAAALEED'); _loading = false; _roomId = null; _matches = []; }
bool get isLoading => _loading; List get matches => _matches;
void updateMatch(Match match){ _matches.add(match); print(_matches.length); notifyListeners(); }
void setupHubConnection(String roomId) async { print("Matches: ${_matches.length}"); print("Loading?: $_loading"); _loading = true; _onConnectionJoinHub = await OnConnectionJoin(roomId, onNewConnection).initialize(); notifyListeners(); }
void onNewConnection(List
void switchLoading() => _loading = !_loading;
Future selectRoomByRoomID(String roomID) async {
print(roomID);
var request = await RoomService.getRoomFromIdentifier(roomID);
}
void clearViewModel(){ _loading = false; _roomId = null; _matches = []; }
}`
View
`import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:provider/provider.dart'; import 'package:racket_match/Widgets/match_graphic.dart'; import 'package:racket_match/models/match.dart'; import 'package:racket_match/models/player.dart'; import 'package:racket_match/models/room.dart'; import 'package:racket_match/services/hub_clients/on_connection_join.dart'; import 'package:racket_match/view_models/room_instance_view_model.dart'; import 'package:racket_match/widgets/match_graphic_list.dart';
//ignore: must_be_immutable class RoomInstance extends StatefulWidget{ RoomInstance.optional({Key? key}) : super(key: key); RoomInstance({Key? key, required this.room}) : super(key: key);
late Room room;
@override State createState() => _RoomInstanceState();
}
class _RoomInstanceState extends State with WidgetsBindingObserver{
bool hubActive = false;
late RoomInstanceViewModel roomInstanceViewModel;
@override void dispose() { print('Disposed'); // This should really be fixed!? roomInstanceViewModel.clearViewModel(); super.dispose(); }
@override Widget build(BuildContext context) { var we = MediaQuery.of(context).size.width; var he = MediaQuery.of(context).size.height;
} }`
Expected behavior The View Model should be cleared when exiting a given view, getting it through Provider.of(context);
Is this normal behaviour, is this supposed to happen?