【问题标题】:Flutter http post data when click the button单击按钮时颤动http发布数据
【发布时间】:2021-05-18 06:58:32
【问题描述】:

当按下按钮时,我想发送 post 数据“1”或“2”int 值(http 包)。我该怎么做?

我的主页画面是这样的;

  import 'package:http/http.dart' as http;
 
      body: Container(
        child: Column(
          children: [
            TextButton(
              onPressed: () async {
                var url = Uri.parse(
                    'http://test.com/control.php');
                var response = await http.post(url, body: {'status': 2});
                print('Response status: ${response.statusCode}');
                print('Response body: ${response.body}');
              },
              child: Text('Open'),
            ),
            TextButton(
              onPressed: () async {
                var url = Uri.parse(
                    'http://test.com/control.php');
                var response = await http.post(url, body: {'status': 2});
                print('Response status: ${response.statusCode}');
                print('Response body: ${response.body}');
              },
              child: Text('Close'),
            ),
          ],
        ),
      ),

对不起我的英语,谢谢你的帮助!

【问题讨论】:

    标签: flutter http


    【解决方案1】:

    你可以这样做。

    final body = jsonEncode({'status': 1});
    
    final url = Uri.http('http://test.com', 'control.php');
    
    final response = await http.post(
        url,
        headers: {'Content-Type': 'application/json'},
        body: body
        );
    

    【讨论】:

      猜你喜欢
      • 2020-04-12
      • 1970-01-01
      • 2013-04-20
      • 2020-09-24
      • 2023-04-03
      • 2019-02-23
      • 2019-11-23
      • 2015-03-06
      相关资源
      最近更新 更多