Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: how to Pop multiple parameters

Tags:

flutter

dart

This is problably sthg simple, I would like to pop a widget and to pass different parameters, here is what I have :

 class MyScreen extends StatefulWidget {
   final String param0;
   final String param1;
   final String param2;

   MyScreen(param0,param1,param2);

   @override
   MyState createState() => new MyScreenState();
}
...

I would like to go back to this widget passing parameters, something like this :

Navigator.pop(context, "NewParam0", "NewParam1", "NewParam2");

but it doesn't work.

I can pop with 1 parameter and the context, but it doesn't work with multiple parameters,

Any idea?

like image 910
Julien Avatar asked Sep 13 '25 23:09

Julien


1 Answers

Yep, the solution is simple! The way I've dealt with this is by popping an object. For instance, a Map<String, String>:

Navigator.pop(context, 
  {"NewParam0": "param0value", "NewParam1": "param1value", "NewParam2": "param2value"}
);

(see https://api.flutter.dev/flutter/widgets/Navigator/pop.html and https://flutter.dev/docs/cookbook/navigation/returning-data)

You could also make a lightweight class to pop that will populate default params, etc. if you have a lot of complexity to pass back and forth, though at that point, I might try to rework my state management a little bit.

like image 74
thmsdnnr Avatar answered Sep 16 '25 13:09

thmsdnnr