首页 > 解决方案 > 使用 Image.file 构造函数创建对象时出错

问题描述

我正在尝试使用 Image.file () 创建带有图像的小部件,但在尝试时发生错误

代码:

import 'package:flutter/material.dart';
import 'dart:io';

void main() {
  runApp(StateApp());
}

class StateApp extends StatefulWidget {
  State<StatefulWidget> createState() {
    return Application();
  }
}

class Application extends State<StateApp> {
  Widget build(BuildContext con) {
    return MaterialApp(
      home:Scaffold(
        appBar: AppBar(
          title: Text('Title'),
        ),
        body: Image.file(File("/home/suchin/1.png")),
      ),
    );
  }
}

错误日志:

══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
The following UnsupportedError was thrown resolving an image codec:
Unsupported operation: _Namespace

When the exception was thrown, this was the stack:

...

Path: /home/user/1.png
════════════════════════════════════════════════════════════════════════════════════════════════════

这是什么原因?

标签: flutterio

解决方案


你从哪里得到路径?看起来不像移动路径

试试这个来挑选图像,然后你可以显示结果


推荐阅读