What is the best way to pass data or arguments between flutter app screens?

902

Typically the best way is to use app state management. You edit the state before switching routes and read the state on the new screen. If done with persistence this also has the benefit of being able to restore your app to the last state after being closed (which happens frequently on phones).

Depending on the complexity of your app state you could use the flutter built in state management or an addon like redux.

Redux requires more boilerplate coding but also offers more flexibility and for very large apps that is often required.

Here are a few interesting articles about the subject.

Official docs for state management

Redux or not

Help with choosing state management

Alternatives are to pass arguments on route navigation. This can be done either as part of the route string (which doesn't work with static route strings) or with a MaterialPageRoute for example (see this answer).

There is also package that makes passing parameters easy: https://pub.dartlang.org/packages/navigate.

An example with the navigate package looks like this (taken from this github issue):

  Map arg =  {"transactionType":TransactionType.fromLeft,"replaceRoute":ReplaceRoute.thisOne};
    Navigate.navigate(context,
                          "home",
                          transactionType:TransactionType.fromLeft ,
                          replaceRoute: ReplaceRoute.thisOne,
                          arg: arg
                          );
Share:
902
Salma
Author by

Salma

Updated on December 08, 2022

Comments

  • Salma
    Salma over 1 year

    In order to send an argument from one screen to the other I have to set this argument as a field in the widgets class. With many arguments being passed down to several widgets I think this might cause a problem for bigger applications.

    What is the best way to pass data between screens efficiently?