flutter NetworkImage(url)不工作|无效的图像数据

enxuqcxy  于 2023-05-19  发布在  Flutter
关注(0)|答案(1)|浏览(209)

我正在尝试使用NetwokImage显示配置文件图像。我已经检查了浏览器中的链接,它的工作。我尝试log在终端的链接和它的工作。但照片还是没有显示出来。

这里是代码

Widget header() {
      return Container(
        margin: EdgeInsets.only(
          top: defaultMargin,
          left: defaultMargin,
          right: defaultMargin,
        ),
        child: Row(
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "Hello, ${user.name}!",
                    style: primaryTextStyle.copyWith(
                        fontSize: 24, fontWeight: semiBold),
                  ),
                  const SizedBox(
                    height: 2,
                  ),
                  Text("@${user.username}",
                      style: subtitleTextStyle.copyWith(
                          fontSize: 16, fontWeight: regular))
                ],
              ),
            ),
            Container(
              height: 54,
              width: 54,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                image: DecorationImage(
                  image: NetworkImage(user.profilePhotoUrl),
                ),
              ),
            )
          ],
        ),
      );
    }

这是日志

[log] https://ui-avatars.com/api/?name=s&color=7F9CF5&background=EBF4FF

此为错误信息

======== Exception caught by image resource service ================================================
The following _Exception was thrown resolving an image codec:
Exception: Invalid image data

When the exception was thrown, this was the stack: 
#0      _futurize (dart:ui/painting.dart:6408:5)
#1      ImageDescriptor.encoded (dart:ui/painting.dart:6265:12)
#2      instantiateImageCodecFromBuffer (dart:ui/painting.dart:2150:60)
#3      PaintingBinding.instantiateImageCodecFromBuffer (package:flutter/src/painting/binding.dart:153:15)
#4      NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:135:22)
<asynchronous suspension>
Image provider: NetworkImage("https://ui-avatars.com/api/?name=s&color=7F9CF5&background=EBF4FF", scale: 1.0)
Image key: NetworkImage("https://ui-avatars.com/api/?name=s&color=7F9CF5&background=EBF4FF", scale: 1.0)
====================================================================================================

怎么修?

6ljaweal

6ljaweal1#

总体设置的问题是,我检查了www.example.com API的输出ui-avatars.com,它返回一个SVG,而不是jpg或png。
Flutter Images不支持SVG,你需要使用flutter_svg库来显示SVG。

final Widget networkSvg = SvgPicture.network(
  'https://ui-avatars.com/api/?name=s&color=7F9CF5&background=EBF4FF',
  semanticsLabel: 'A shark?!',
  placeholderBuilder: (BuildContext context) => Container(
      padding: const EdgeInsets.all(30.0),
      child: const CircularProgressIndicator()),
);

我从图书馆的文档中取了这个例子。

相关问题