lucky
lucky

Reputation: 315

How set multi item in post request flutter

I'm working on flutter project . I'm trying to create post request as you see in postman .

enter image description here

My problem is in "revision_type' key . It's integer value but in the app refer to 4 values :

0 ==> videnge
1 ==> visite technique
2 ==> assurance véhicule
3 ==> autre

In fact the user will set a string value ( DropdownMenuItem) not integer as you see in postman.

How i can synchronise between string and integer value in post request ?

my code :

Future<String> setRevision(
      String revision_type,
      String revision_title,
      String revision_date,
      String revision_location,
      String kilometrage_pour_vidange) async {
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    String token = localStorage.getString('access_token');
    await checkInternet();
    Map<String, String> headers = {
      'Content-type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token'
    };
    Map<String, String> body = {
      'revision_type': revision_type,
      'revision_title': revision_title,
      'revision_date': revision_date,
      'revision_location': revision_location,
      'kilometrage_pour_vidange': kilometrage_pour_vidange,
    };

    try {
      final response = await http.post(Uri.parse(ApiUtil.SET_REVISION),
          headers: headers, body: jsonEncode(body));
      final responseBody = jsonDecode(response.body);

      print(responseBody);

      var data = body['message'];
      switch (response.statusCode) {
        case 200:

 Row(children: [
                          Expanded(
                              child: DropdownButtonFormField(
                            decoration: InputDecoration(
                              hoverColor: Colors.white,
                              //contentPadding: EdgeInsets.only(left: 10, right: 15, top: 15),
                              labelText: 'Type',
                              alignLabelWithHint: true,
                              labelStyle: TextStyle(
                                color: kPrimaryColor,
                              ),
                              enabledBorder: UnderlineInputBorder(
                                borderSide: BorderSide(color: Colors.grey),
                              ),
                            ),
                            dropdownColor: Colors.white,
                            value: status,
                            items: <DropdownMenuItem>[
                              DropdownMenuItem(
                                value: 'videnge',
                                child: InkWell(
                                  child: Text('videnge'),
                                  hoverColor: Colors.indigo,
                                ),
                              ),
                              DropdownMenuItem(
                                value: 'visite technique',
                                child: Text('visite technique'),
                              ),
                              DropdownMenuItem(
                                value: 'assurance véhicule',
                                child: Text('assurance véhicule'),
                              ),
                              DropdownMenuItem(
                                value: 'autre',
                                child: Text('autre'),
                              ),
                            ],
                            onChanged: (value) {
                              setState(() {
                                status = value;
                              });
                            },
                          )),
                        ]),

void setRevision() async {
    if (_formKey.currentState.validate()) {
      String kilometrage_pour_vidange = _KilometrageController.text;
      String revision_type = status;
      String revision_title = _eventController.text;
      String revision_location = _EmplacementController.text;
      String revision_date = _DateController.text;
      // print(revision_type);

      revisionApi
          .setRevision(
        revision_type,
        revision_title,
        revision_date,
        revision_location,
        kilometrage_pour_vidange,
      )
          .then((data) {
        if (data != null) {
          Navigator.pop(context);

          Navigator.push(
              context, MaterialPageRoute(builder: (context) => Revision()));
        }

      }).catchError((error) {
        ScaffoldMessenger.of(context)
            .showSnackBar(SnackBar(content: Text(error.toString())));
      });
      setState(() {});
    }
  }

Upvotes: 0

Views: 217

Answers (1)

Abdelazeem Kuratem
Abdelazeem Kuratem

Reputation: 1706

Just replace the value with the int status refers to this DropdownMenuItem

for an example:

items: <DropdownMenuItem>[
          DropdownMenuItem(
            value: 0,
            child: InkWell(
              child: Text('videnge'),
              hoverColor: Colors.indigo,
            ),
          ),
          DropdownMenuItem(
            value: 1,
            child: Text('visite technique'),
          ),
          DropdownMenuItem(
            value: 2,
            child: Text('assurance véhicule'),
          ),
          DropdownMenuItem(
            value: 3,
            child: Text('autre'),
          ),
        ],

Upvotes: 1

Related Questions