Provider was used after being disposed when pop all routes with pushAndRemoveUntil #701
Unanswered
GokayFinfree
asked this question in
Q&A
Replies: 1 comment 1 reply
-
@GokayFinfree hey bro, I got the same here, how did you fix this issue |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Describe the bug
Hi, in my app I use pushAndRemoveUntil to pop all routes and push a new one. In one of those routes, I create a changeNotifierProvider and pass it to a few routes deeper. The problem is when I call pushAndRemoveUntil(with (route)=>false) I get "════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
A ValueHolder was used after being disposed." exception.
As far as I understand from the source code of pushAndRemoveUntil it pops routes top to bottom so this shouldn't happen. In my app, I get this error after 5 or 6 routes in my app but in the little app, I created to reproduce this user have to push at least 15 routes(couldn't understand the relation btw). Here is code to reproduce ;
To Reproduce
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage3(),
);
}
}
class ValueHolder extends ChangeNotifier {
ValueHolder(this.data);
T data;
void setData(T newData) {
data = newData;
notifyListeners();
}
}
class MyHomePage3 extends StatelessWidget {
const MyHomePage3({Key? key}) : super(key: key);
@OverRide
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: () => ValueHolder("Base Provider Value"),
builder: (context, child) => Scaffold(
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
const Text(
'Provider created in this route',
),
GestureDetector(
onTap: () => Navigator.of(context).push(
MaterialPageRoute(
builder: () => ChangeNotifierProvider<ValueHolder>.value(
value: context.read<ValueHolder>(),
child: const MyHomePage(),
),
),
),
child: const Text('START'))
]))));
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@OverRide
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GestureDetector(
onTap: () {
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => const MyHomePage2()), (route) => false);
},
child: const Text("Press to remove all routes and push new route"))),
floatingActionButton: FloatingActionButton.extended(
onPressed: () => Navigator.of(context).push(MaterialPageRoute(
builder: (_) => ChangeNotifierProvider<ValueHolder>.value(
value: context.read<ValueHolder>(),
child: const MyHomePage(),
))),
label: const Text("Press to push new routes with the same provider"),
),
);
}
}
class MyHomePage2 extends StatelessWidget {
const MyHomePage2({Key? key}) : super(key: key);
@OverRide
Widget build(BuildContext context) {
return const Scaffold(body: SizedBox());
}
}
Expected behavior
What I expect is to dispose of previous routes and providers without any exception.
Beta Was this translation helpful? Give feedback.
All reactions