Repository: flutter
Issue: [tests] Widget is unexpectedly disposed on navigation
Labels: a: tests,framework,f: integration_test,has reproducible steps,P2,found in release: 2.2,found in release: 2.4,team-framework,triaged-framework

Note: Everything works fine on a normal app run, the issue is happening only while running integration test.

 We are using Flutter Channel stable, 2.2.3 and for integration test we are using the one from flutter SDK:  “integration_test: sdk: flutter”

After navigating from Screen#1 to Screen#2 through `Navigator.of(context).pushNamed(ScreenTwo, arguments: counterProvider);` the dispose() method of Screen#1 is called. We are using Provider for state management, the provider is created on Screen#1 and is passed to Screen#2.  Now the below error is shown:

Error: A CounterProvider was used after being disposed. Once you have called dispose() on a CounterProvider, it can no longer be used.

And this is happening because the Screen#1 page is being disposed after navigating to Screen#2 page.
  Does anyone faced this issue or does anyone has any idea how we can prevent the disposed method being called after navigating? 

It's only happening during integration test.

The link to the project: https://bitbucket.org/UrimGashiABC/test-dispose-issuesample/src/master/

Screen#1 class:

<details> 
<summary>code snippet</summary> 

```dart
class ScreenOnePage extends StatefulWidget {
  @override
  _ScreenOnePageState createState() => _ScreenOnePageState();
}

class _ScreenOnePageState extends State<ScreenOnePage> {

  @override
  Widget build(BuildContext context) {
    CounterProvider counterProvider = Provider.of<CounterProvider>(
      context,
      listen: true,
    );
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Screen #1',
            ),
            SizedBox(
              height: 150,
            ),
            Text(
              '${counterProvider.counter}',
              style: Theme.of(context).textTheme.headline4,
            ),
            SizedBox(
              height: 150,
            ),
            TextButton(
              key: const Key('screen_one_text_button'),
              onPressed: () {
                Navigator.of(context).pushNamed(ScreenTwo, arguments: counterProvider);
              },
              child: Text('Go to screen#2'),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        key: const Key('screen_one_floating_button'),
        onPressed: () => counterProvider.increaseCounter(),
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), //
    );
  }
}
```

</details>

Screen#2 class:

<details> 
<summary>code snippet</summary> 

```dart
class ScreenTwoPage extends StatefulWidget {
  @override
  _ScreenTwoPageState createState() => _ScreenTwoPageState();
}

class _ScreenTwoPageState extends State<ScreenTwoPage> {
  @override
  Widget build(BuildContext context) {
    CounterProvider counterProvider = Provider.of<CounterProvider>(
      context,
      listen: false,
    );
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                'Screen #2',
                key: const Key('screen_two_title'),
              ),
              SizedBox(
                height: 150,
              ),
              Text(
                'Counter: ${counterProvider.counter}',
                key: const Key('screen_two_counter'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
```

</details>
Provider class:

```dart
class CounterProvider extends ChangeNotifier{
    int counter = 0;

    void increaseCounter(){
      counter++;
      notifyListeners();
    }
}
```

Test class:

<details> 
<summary>code snippet</summary> 

```dart
void main(){
    IntegrationTestWidgetsFlutterBinding.ensureInitialized();

    testWidgets('counter test', (WidgetTester tester) async {
      app.main();
      await tester.pumpAndSettle();
      final Finder screenOneButton = find.byKey(const Key('screen_one_text_button'));
      final Finder screenOneFloatingButton = find.byKey(const Key('screen_one_floating_button'));
      final Finder screenTwoTitle = find.byKey(const Key('screen_two_title'));
      final Finder screenTwoCounter = find.byKey(const Key('screen_two_counter'));

      await tester.tap(screenOneFloatingButton);
      await tester.tap(screenOneButton);

      await tester.pumpAndSettle();

      expect(screenTwoTitle, findsOneWidget);
    });

}
```

</details>