modular icon indicating copy to clipboard operation
modular copied to clipboard

Error when assigning arguments during consecutive navigations

Open DimensaSpaki opened this issue 1 year ago • 0 comments

Describe the bug In the case of multiple consecutive navigations, calling Modular.to.pushNamed followed by another, the arguments from the last navigation is used for all the others. If the argument of the last navigation is null, null is used along.

To Reproduce

import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';

void main() {
  runApp(ModularApp(module: AppModule(), child: const AppWidget()));
}

class AppWidget extends StatelessWidget {
  const AppWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerDelegate: Modular.routerDelegate,
      routeInformationParser: Modular.routeInformationParser,
    );
  }
}

class AppModule extends Module {
  @override
  void routes(r) {
    r.child('/', child: (_) => const HomePage());
    r.child('/first', child: (_) => Page(title: r.args.data));
    r.child('/second', child: (_) => Page(title: r.args.data));
    r.child('/third', child: (_) => Page(title: r.args.data));
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home')),
      body: Center(
        child: ElevatedButton(
          onPressed: navigateThroughPages,
          child: const Text('Navigate'),
        ),
      ),
    );
  }

  void navigateThroughPages() {
    Modular.to.pushNamed('/first', arguments: 'First Page');
    Modular.to.pushNamed('/second', arguments: 'Second Page');
    Modular.to.pushNamed('/third', arguments: 'Third Page');
  }
}

class Page extends StatelessWidget {
  const Page({super.key, required this.title});

  final String title;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(title)),
      body: Center(
        child: ElevatedButton(
          onPressed: Modular.to.pop,
          child: const Text('Pop'),
        ),
      ),
    );
  }
}

Expected behavior It was expected that each navigation would use the arguments provided to it.

Screenshots Observe the route and compare it with the text in the app bar, which is passed as Modular.pushNamed arguments.

image image image image

The screenshots are from a web application just to make it easier to visualize the route, but this also occurs on other platforms.

I tested the same logic with go_router and got the expected behavior.

image image image

DimensaSpaki avatar Nov 22 '24 18:11 DimensaSpaki