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

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.');
}

最佳答案

我不确定此代码是否打算用作函数或类。如果将其视为功能,则可以执行以下操作

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", "[email protected]", "avatar" )来调用它。
我也更喜欢使用驼峰式命名函数(用postSocialData代替PostSocialData)和pascal命名类。
我希望这可以帮助你,
干杯

关于flutter - 如何为Mysql/Php Post Request的Future添加参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62739232/

10-12 00:27