首页 > 解决方案 > 如何为 Mysql/Php 发布请求的 Future 添加参数?

问题描述

我正在尝试使用 Flutter/dart 中的 future 将用户名、电子邮件和 URL 插入 php/mysql 数据库。有什么方法可以向这个未来添加参数,以便可以从另一个页面调用 PostSocialData(),并使用用户名、电子邮件和 URL 作为参数?
这是我尝试过的:

Future<void> PostSocialData() async {

 String currentname;
 String currentemail;
 String currentavatar;

 PostSocialData({
   this.currentname,
   this.currentemail,
   this.currentavatar,
 });

  final userinfo = "http://example.com/postSocialUser.php?currentname=" 
   + currentname + 
   "&currentemail=" 
   + currentemail + 
   "&currentvatar=" 
   + currentavatar;

  final response = await get(userinfo);

  if (response.statusCode == 200) {
    print(response);
  } else {
    throw Exception('We were not able to successfully post social data.');    
}

标签: flutterdart

解决方案


我不确定这段代码是否打算成为一个函数或类。如果您将其视为一个函数,您可以执行以下操作

Future<void> PostSocialData(String name, String email, String avatar) async {
  final url = "http://example.com/postSocialUser.php?currentname=$name&currentemail=$email&currentvatar=$avatar";

  final response = await get(url);

  if (response.statusCode == 200)
    print(response);
  else 
    throw Exception('We were not able to successfully post social data.');    
}

拥有此功能,您可以通过执行以下操作来调用它await PostSocialData( "George", "george@gmail.com", "avatar" )

此外,我更喜欢使用骆驼大小写来命名函数(postSocialData而不是PostSocialData),使用帕斯卡大小写来命名类。

我希望这可以帮助你,干杯


推荐阅读