简体   繁体   中英

How to pass 2 Arguments in flutter?

I want to pass 2 arguments to my MainPage() here is my code:

final String NumID = ModalRoute.of(context).settings.arguments.toString();
                    final IDNum = NumID.toString();
                    final SmsCode = _controller.text;
                    final url = 'https://*******/api/User/$IDNum?sms=$SmsCode';
                    Future fetchSMS() async {
                      final response = await http.get(url);
                      if (response.statusCode == 200) {
                        return jsonDecode(response.body);
                      } else {
                        throw Exception('Failed');
                      }
                    }
fetchSMS().then((resaultOfFeture) {
                      if(resaultOfFeture == true){
                        final snackBar = SnackBar(content: Text('با موفقیت وارد شدید',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              color: Colors.white,
                              fontFamily: "IranSans",
                              fontSize: 18
                          ),
                        ),
                        );
                        ScaffoldMessenger.of(context).showSnackBar(snackBar);
                        Navigator.of(context).push(MaterialPageRoute(
                          builder: (context) => MainPage(),
                          settings: RouteSettings(
                            arguments: resaultOfFeture,
                          ),
                        ));

except the resaultOfFeture, I want to pass NumID too. any idea?

You can pass a map as argument:

var args= {'resaultOfFeture': resaultOfFeture ,'anotherThing':'otherValue'}; 

settings: RouteSettings(arguments: args),

On the other screen you can get the values:

var data = ModalRoute.of(context).settings.arguments;

var result = data['resaultOfFeture'];

Navigator's ' arguments ' param accepts an object, so you can pass an object containing both resaultOfFeture and NumID, for eg,

arguments: {'res': resaultOfFeture, id: NumID ,}

or you can also define a class containing these two fields as its member, then pass this object. In your MainPage you can retrieve theis using ModalRoute.of(context)..settings.arguments

Please refer: https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments#1-define-the-arguments-you-need-to-pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM