Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

arrays - Pass list to http flutter

I would like to pass these kind of params to server

enter image description here

In PageA, when done button is clicked, I able to get this kind of output

I/flutter ( 4802): {material[0][name]: Cement OPC}
I/flutter ( 4802): {material[0][quantity]: 56}
I/flutter ( 4802): {material[0][type]: Bag}
I/flutter ( 4802): {material[8][name]: dd}
I/flutter ( 4802): {material[8][quantity]: 22}
I/flutter ( 4802): {material[8][type]: Box}

Here the code

 IconButton(
                onPressed: () {
                  for (var i = 0; i < _controllers.length; i++) {
                    if (_controllers[i].text != "") {
                      Map<String, dynamic> test = {
                        "material[$i][name]": nameLabel[i]
                      };
                      print(test);
                      Map<String, dynamic> test2 = {
                        "material[$i][quantity]": _controllers[i].text
                      };
                      print(test2);
                      Map<String, dynamic> test3 = {
                        "material[$i][type]": quantityLabel[i]
                      };
                      print(test3);
                    }
                  }
                },
                icon: Icon(Icons.done))

My problem is how can I pass these output to http ? Is it using List or HashMap ? I'm kinda lost although it sounds simple...

question from:https://stackoverflow.com/questions/65934179/pass-list-to-http-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Look below it might be help you

List<String> names = ["jsbc", "snjc", "scnj"];
List<String> quantity = ["1", "2", "3"];
List<String> type = ["wqew", "dcvd", "dw"];

Map<String, dynamic> test = Map();
    for (var i = 0; i < names.length; i++) {
      var data = {
        "material[$i][name]": names[i],
        "material[$i][quantity]": quantity[i],
        "material[$i][type]": type[i]
      };
      test.addAll(data);
    }
    print(test);

Output:

{material[0][name]: jsbc, material[0][quantity]: 1, material[0][type]: wqew, material[1][name]: snjc, material[1][quantity]: 2, material[1][type]: dcvd, material[2][name]: scnj, material[2][quantity]: 3, material[2][type]: dw}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...