flutter - 我可以从函数返回全局变量吗?
问题描述
我想将路径字符串作为全局变量返回...我不能使用这个返回的路径,它说它是动态的:
openGallery(BuildContext context) async {
var picture = await ImagePicker.pickImage(source: ImageSource.gallery);
this.setState(() {
imageFile = picture;
});
Navigator.of(context).pop();
Directory patid = await getApplicationDocumentsDirectory();
String path = patid.path;
print("$path");
final File newImage = await imageFile.copy('$path/name.jpg');
return (path);
}
解决方案
将您的功能签名更改为:
Future<String> openGallery(BuildContext context) async { //Added return type
//Your code here...
}
并且不要忘记调用 await
String path = await openGallery(context);
推荐阅读
- c++ - 在微服务 C++ 中运行 gRPC 服务器
- amazon-dynamodb - 具有全局二级索引的 DynamoDB 查询抛出“ValidationException:不支持查询键条件”
- database - 条件连接的 Postgres 索引
- c# - xml 的 noNamespaceSchemaLocation 中缺少 xsi
- python - 如果为空,则带有标题的 Scrapy 导出
- javascript - 生成路由“未定义”时出错:找不到此页面
- monitoring - Nagios 不会为插件创建性能数据
- java - 使用 docx4j 将 HTML 字符串转换为 docx 图像未嵌入 docx
- python - 如何获取堆积条形图的总高度以注释上面的值
- javascript - 如何将数据发送到服务器,从 url 中删除参数并使用新的 url 重新加载窗口 Javascript、jQuery 和 PHP