2

I'm new to flutter and start developing my first app in flutter but facing issue in routing data from one dart file to another. The example of my code is given below.

I have define route in main.dart like this

final routes = <String, WidgetBuilder>{
    Login.tag: (context) => new Login(),
    HomeView.tag: (context) => new HomeView(),
  };

I have two separate file for login and homeview

In Login file the tag is also defined and as the user authenticate it will navigate to HomeView.

// tag defination
static String tag = 'login';


//Navigate on successfull login
Navigator.of(context).pushNamed(HomeView.tag);

So, How can I pass the userdata in that navigator command to HomeView that was fetch using REST Api in JSON format.

Muhammad Ikhlas
  • 78
  • 2
  • 10
  • Take a look at this https://stackoverflow.com/a/49445888/8097803 – Doge Apr 09 '18 at 17:51
  • Possible duplicate of [How do I pass non-string data to a named route in Flutter?](https://stackoverflow.com/questions/47419908/how-do-i-pass-non-string-data-to-a-named-route-in-flutter) – Doge Apr 09 '18 at 17:55
  • This answer may help you https://stackoverflow.com/a/44729625. I am using the fluro package for routing purpose and I am able to pass the Json data as a string as well. Please let me know if you requires a sample with the same – Shyju M Apr 10 '18 at 11:35
  • @ShyjuM Please can you provide me the sample code that will be a great help for me to understand how to use it. – Muhammad Ikhlas Apr 11 '18 at 10:00

2 Answers2

4

I am using the package fluro for routing purpose in my flutter app. We can pass JSON data from one screen to another as explained below

Add the dependency

dev_dependencies:
  fluro: ^1.2.1

Define a router

Router router = new Router();

Define the route, where 'data' is your Json string to be passed

router.define('home/:data', handler: new Handler(
      handlerFunc: (BuildContext context, Map<String, dynamic> params) {
        return new Home(params['data'][0]);
      }));

Navigate as below

var bodyJson = '{"buyerId":1281,"orgId":3041}';
router.navigateTo(context, '/home/$bodyJson');

You can receive the Json string in your Home screen.

Sample project is available here

Shyju M
  • 9,387
  • 4
  • 43
  • 48
  • Hi, I tested it and working fine but when i sent a dynamic JSON data to it. It just sent me a null response. I also print the response is console but it is not null. Can you explain me how can I achieve it. – Muhammad Ikhlas Apr 17 '18 at 04:24
  • try this way (without fluro) : Navigator.push( context, new MaterialPageRoute(builder: (context) => new SecondScreen('Data to be passed')), ); – Shyju M Apr 25 '18 at 06:17
  • Passing data as JSON string might break if it contains `?`. – Chirag Mittal Apr 28 '20 at 07:33
0

@MuhammadIkhlas Shyju's solution should work fine. You get null because you passed in an object instance. Convert your object to string first. Like so:

var bodyJson = json.encode(yourObject.toJson())
router.navigateTo(context, '/home/$bodyJson')
function1983
  • 971
  • 11
  • 14