flutter - 如何为 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 +
"¤temail="
+ currentemail +
"¤tvatar="
+ 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¤temail=$email¤tvatar=$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
),使用帕斯卡大小写来命名类。
我希望这可以帮助你,干杯
推荐阅读
- google-apps-script - 导出带有超链接到工作表的 Google 日历活动
- powerbi - 将 MDX 查询加载到 PowerBI 时,将其加载到 SSMS 时出现问题,没有问题
- python - 出现错误“UnboundLocalError:分配前引用的局部变量'start_time'”
- javascript - 如何保持所有 activeDots 始终“活跃”而不是悬停在数据点上(重新图表)
- javascript - 将对象数组转换为一个逗号分隔的对象
- java - 如何在 Java 中修复 NoSuchMethodError
- google-cloud-platform - 错误:构建步骤 0“gcr.io/cloud-builders/docker”失败:退出状态 1
- java - Android Studio 3.4 - 无法调试 onClickListeners
- c# - WinForms ReportViewer HTML 交互不工作
- dart - 每个平台依赖项