我可以 return 函数中的全局变量吗?

Can I return global variable from function?

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

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