首页 > 解决方案 > 我可以从函数返回全局变量吗?

问题描述

我想将路径字符串作为全局变量返回...我不能使用这个返回的路径,它说它是动态的:

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

标签: flutterdartglobal-variablesfuture

解决方案


将您的功能签名更改为:

Future<String> openGallery(BuildContext context) async { //Added return type
  //Your code here...
}

并且不要忘记调用 await

String path = await openGallery(context);

推荐阅读