首页 > 解决方案 > 使用 SvgPicture.assets 加载图片时出现颤振问题

问题描述

我正在尝试将图像加载到我的颤振应用程序中,并且由于某些原因,我收到了一个我不知道如何修复的错误

这是我的代码:

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

class LogInPage extends StatefulWidget {
  const LogInPage({Key? key}) : super(key: key);

  @override
  _LogInPageState createState() => _LogInPageState();
}

class _LogInPageState extends State<LogInPage> {
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;

    double viewInsert = MediaQuery.of(context).viewInsets.bottom;
    double defaultLoginSize = size.height - (size.height * 0.2);
    double defaultRegisterSize = size.height - (size.height * 0.1);

    return Scaffold(
      body: Stack(
        children: [
          Align(
            alignment: Alignment.center,
            child: SingleChildScrollView(
              child: Container(
                width: size.width,
                height: defaultLoginSize,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      'Welcome',
                      style:
                          TextStyle(fontWeight: FontWeight.bold, fontSize: 24),
                    ),
                    SizedBox(height: 40),
                    SvgPicture.asset('assets/Image1.png'), // this is where I am trying to load 
                                                              // the picture
                    SizedBox(height: 40),
                    Container(
                      margin: EdgeInsets.symmetric(vertical: 10),
                      padding:
                          EdgeInsets.symmetric(horizontal: 20, vertical: 5),
                      width: size.width,
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

这是我得到的错误:

SVG 捕获的
异常 解析单帧图片流时引发以下 _Exception:
异常:FormatException:意外的扩展字节(偏移量 0)
图片提供者:ExactAssetPicture(name: "assets/MyBarber3.png", bundle: null, colorFilter: >null)
图片键:AssetBundlePictureKey(bundle: PlatformAssetBundle#05902(), name:
"assets/MyBarber3.png", colorFilter: null) 这是我的 ymal 文件:

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^1.4.0
  firebase_database: ^7.1.2
  firebase_auth: ^3.0.1
  cloud_firestore: ^2.4.0
  firebase_storage: ^10.0.1
  flutter_svg: ^0.22.0
  google_fonts: ^2.1.0
  cupertino_icons: ^1.0.2

assets:
  - assets/

有人知道我该如何解决吗?

标签: flutter

解决方案


您的代码清楚地表明您正在尝试在 SvgPicture.asset 函数中加载 png

SvgPicture.asset('assets/Image1.png'), // here

您可以使用加载您的 png 图像

Image.asset('assets/Image1.png')  

或者

AssetImage('assets/Image1.png')

否则放置svg图片,它工作正常。


推荐阅读