首页 > 解决方案 > 颤振异常生成器不断被调用

问题描述

我正在使用颤动的 Image.file() 在我的应用程序中显示图像。我还使用 errorBuilder 来处理任何崩溃并向用户显示消息。

执行这些步骤时遇到问题。

  1. 加载一个好的图像
  2. 将损坏的图像加载到同一个 Image.File() 小部件中
  3. 将原始好的图像加载回相同的 Image.File() 小部件

在传入损坏的照片(步骤 2)后,似乎每次文件更改都会导致显示错误生成器,而不是新的好图像。

如果我没有在第 2 步中传入损坏的照片,则照片会发生应有的变化。这是颤振 Image() 的错误,还是在它进入 errorBuilder 之后我应该做些什么。

这是我目前的设置。

Image.file(
        file,
        fit: BoxFit.cover,
        errorBuilder: (BuildContext context, Object exception, StackTrace stackTrace) {
          print("Failed to initialise the file");
          print(stackTrace);
          //  Once an error occurs it always goes in here 
          return Text("an error occurred");
        },
      );

传入损坏文件时/之后,我收到的所有文件更改的实际错误是

Could not instantiate image codec.

更新

我写了一个 dartpad 来显示我遇到的问题。

https://dartpad.dev/98c2dacb481c088dfd2e5bee490f45ed

如果你点击

  1. 好形象
  2. 好形象 2
  3. 好形象
  4. 好形象 2

图像循环正确...有效。

如果您然后单击将尝试加载损坏的 jpeg 文件的“损坏的图像”,则会触发错误生成器。

如果您然后单击“良好图像”或“良好图像 2”,它们将不再构建,并且图像每次都被卡住加载错误生成器......我怎样才能让它再次加载其中一个好图像?

如果我不清楚,请告诉我,我会添加更多信息:)

非常感谢

标签: flutterdart

解决方案


您可以在下面复制粘贴运行完整代码
这个错误可以用 add 修复key: UniqueKey()
我已经用 DardPad 测试它工作正常
代码片段

Image.network(
          _selectedImageURL,
          key: UniqueKey(),

工作演示

在此处输入图像描述

完整代码

import 'package:flutter/material.dart'; 

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  String _goodImageOne =
      "https://upload.wikimedia.org/wikipedia/commons/6/69/June_odd-eyed-cat_cropped.jpg";
  String _corruptImage =
      "https://srv-file16.gofile.io/download/hwTzLI/cat_corrupt.jpg";
  String _goodImageTwo =
      "https://upload.wikimedia.org/wikipedia/commons/c/c7/Tabby_cat_with_blue_eyes-3336579.jpg";

  String _selectedImageURL;

  @override
  void initState() {
    super.initState();
    _selectedImageURL = _goodImageOne;
  }

  void _changeFile(String newUrl) {
    setState(() {
      _selectedImageURL = newUrl;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Image.network(
              _selectedImageURL,
              key: UniqueKey(),
              height: 300,
              width: 200,
              loadingBuilder: (BuildContext context, Widget child,
                  ImageChunkEvent loadingProgress) {
                if (loadingProgress == null) return child;
                return Center(
                    child: CircularProgressIndicator(
                  value: loadingProgress.expectedTotalBytes != null
                      ? loadingProgress.cumulativeBytesLoaded /
                          loadingProgress.expectedTotalBytes
                      : null,
                ));
              },
              errorBuilder: (BuildContext context, Object exception,
                  StackTrace stackTrace) {
                return Text("Cannot display url");
              },
            ),
            Expanded(child: Container()),
            RaisedButton(
              onPressed: () => _changeFile(_goodImageOne),
              child: Text("Good Image"),
            ),
            RaisedButton(
              onPressed: () => _changeFile(_corruptImage),
              child: Text("Corrupt Image"),
            ),
            RaisedButton(
              onPressed: () => _changeFile(_goodImageTwo),
              child: Text("Good Image 2"),
            ),
          ],
        ),
      ),
    );
  }
}

推荐阅读