【问题标题】:how to pass parameters to web service when click on button in flutter在flutter中单击按钮时如何将参数传递给Web服务
【发布时间】:2018-07-30 10:35:02
【问题描述】:

我是 Flutter 开发的新手。能否请任何人告诉我单击按钮时如何将参数传递给 url。谢谢你。

 import 'package:flutter/material.dart';
    import 'package:minerva_admin/Homescreen.dart';
    import 'dart:async';
    import 'dart:convert';
    import 'package:http/http.dart' as http;


    class Login extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return new LoginState();
      }

    }


    class LoginState extends State<Login> {

      final TextEditingController _userController = new TextEditingController();

      final String url = "http://192.155.1.40:8088/restaurants";
      List data;



      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text("Minerva Money"),
            centerTitle: true,
            backgroundColor: Colors.green,
          ),

          backgroundColor: Colors.white,

          body: new Container(
            alignment: Alignment.topCenter,
            child: new ListView(
              children: <Widget>[
                //image/profi

                //form
                new Container(
                  margin: const EdgeInsets.only(top: 100.0),
                  child: new Column(
                    children: <Widget>[

                      new TextField(
                        controller: _userController,
                        decoration: new InputDecoration(
                            hintText: 'Enter Email/Mobile Number',
                            icon: new Icon(Icons.person)
                        ),
                      ),


                      new Padding(padding: new EdgeInsets.all(10.5)), //add padding

                      new Center(
                        // Login button

                        child: new RaisedButton(

                             onPressed: (){
                          getSWData(_userController.toString());
                        },
                            color: Colors.green,
                            child: new Text("Login",
                                style: new TextStyle(color: Colors.white,
                                    fontSize: 16.9))),


                        // Clear button


                      )
                    ],
                  ),

                ), //form ends here

                new Padding(
                    padding: const EdgeInsets.all(14.0)),


              ],
            ),
          ),


        );
      }
Future<String> getSWData(String email) async {
        var res = await http.get(
            Uri.encodeFull(url), headers: {"Accept": "application/json"});
        print("response" + "$url");
        setState(() {
          var resBody = json.decode(res.body);

          // data = resBody["restos_cities"];
          print("response" + "$resBody");
        });
        return "succees";
      }

    }

任何人都可以解释我如何在单击按钮获取数据时将“电子邮件”参数中的文本字段数据发送到 Web 服务。先感谢您。我在 pubspec 文件中添加了 http: ^0.11.3+16。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    首先,您必须启用RaisedButtononPressed 属性。在那里你必须调用一些函数来传递你定义的TextEditingController中的电子邮件参数:

    onPressed: () {
      yourFunction(_userController.text.toString());
    },
    

    然后,使用从TextField 捕获的参数在“yourFunction”中实现您的 Web 服务调用

    【讨论】:

    • 感谢您的回复。如何将参数传递给 url?我编辑了我的代码
    • 通常“获取”操作会在 url 末尾附加参数(一个简单的连接),但我不知道格式,因为我不知道您的 web 服务的规范。您应该调用 'getSWData(_userController.text.toString())' 而不是 'getSWData(_userController.toString())' 以正确获取输入文本
    猜你喜欢
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2021-01-20
    • 2016-12-27
    • 1970-01-01
    • 2019-05-19
    相关资源
    最近更新 更多