Navjyot Singh
Navjyot Singh

Reputation: 105

How to check the calling route name in Flutter?

I am designing an application in which the same page A which is called from two different pages B and C. Page B is passing an object and page C passing a list to A. I want to check whether A is called from B or C and also manage the different types of arguments passed from screen B and C? It's soo confusing. I am stuck

class A extends StatefulWidget {
  final List<Exercises> data;

  A({
    Key key,
    @required this.data,
  }) : super(key: key);

  @override
  _A createState() => _B();
}

from page B

onTap: () {
           Navigator.pushNamed(context,'/ExerciseScreen',
                              arguments: {text:'abcd',
                                          description:'I am description'});
 },

from page C

List<Class> data;  //its already filled

Navigator.pushNamed(
              context, '/ExerciseScreen',arguments: data
          );

If you want I can attach my router file too

Upvotes: 0

Views: 449

Answers (2)

Yadu
Yadu

Reputation: 3305

Always pass data as a dynamic value Map

arguments = {'dataFromB':{text:'abcd',description:'I am description'}}

arguments = {'dataFromC': dataList}

in A just check the data like this

arguments.contains('dataFromB') //you know its called from B

arguments.contains('dataFromC') //you know its called from C

after access the data from the Map to process

Upvotes: 1

Arnold Parge
Arnold Parge

Reputation: 6867

In my opinion the architecture doesn't seem correct. But if this is the only way then you can do it using one of the below solutions:

  1. If you care about data more than who has called class A then have two different arguments in class A, one for Object and one for List. If object is not null then work on object and if the list is not null then work on the list.

  2. If you care about who has called class A then simply have another argument in A that would be source. When B is calling A pass source as b and if C is calling A then pass source as c

Note: you can use both solutions together as well.

PS: If you could explain in detail what is A,B and C then I can suggest a better architecture.

Upvotes: 1

Related Questions