![](/img/trans.png)
[英]Flutter pass data from one class to another(statefull widget)
[英]How to pass the gallery image to another Statefull Screen in flutter
這是我的代碼:
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = image;
print('Image Path $_image');
});
}
從ImagePicker.pickImage(...)
返回的image
是一個File
對象,您可以將其傳遞給其他小部件,就像使用任何其他對象一樣。 例如,考慮這個片段,其中NewPage
將一個File
圖像對象作為參數:
Future<void> navigateOnImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => NewPage(image)));
}
另一種選擇可能是使用File.readAsBytes
方法將File
對象轉換為更方便的對象以適合您的用例,例如UInt8List
對象。 然后你可以像上面的例子一樣傳遞它。
查看此 flutter.dev 文章,了解有關在導航時傳遞數據的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.