运行apk文件时图像实例不显示,但在桌面上运行良好



我使用Android Studio和linux更新flutter和dart,编译桌面版后一切正常,我可以看到所有的图像。然而,当我在物理设备上运行apk文件时,一些图像不显示。我需要改变图像和setState,而程序正在运行,但atm唯一的方法是在一个命令中从资产创建图像。请告诉我在dart中有什么好的做法可以避免这种情况。

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
FileImage fileImage = FileImage(File('images/sample.jpg'));
Image image1 = Image(image: fileImage);
AssetImage assetImage = AssetImage('images/sample.jpg');
Image image2 = Image(image: assetImage);
Image image3 = Image.file(File('images/sample.jpg'));
Image image4 = Image.asset('images/sample.jpg');

return Scaffold(
body: Column(
children: [
image1, // doesn't show when running on android, but shows on desktop
image2, // doesn't show when running on android, but shows on desktop
image3, // doesn't show when running on android, but shows on desktop
image4,                                // works fine on both platfroms
Image.asset('images/sample.jpg'),      // works fine on both platfroms
Image.file(File('images/sample.jpg')), // works fine on both platfroms
]
),
);

}}

图4,因为您正在从assetBundle中获取数据。

Image.asset('images/sample.jpg'),

图像1,2,3不工作,因为它被称为文件,而它不是。如果他们是在桌面上工作,因为它现在是在调试模块中,其中颤振注入来解决一些调试错误。当您生成已发布的可执行文件时,它将不起作用。