LoginSignup
4
2

More than 1 year has passed since last update.

Flutter webでネットワーク画像が表示されない時の対処法【2022年3月現在】

Last updated at Posted at 2022-03-10

はじめに

Firebaseなどから取ってきた画像のURLを使って、Flutter webで表示するとエラーが出ました。その時の対処法について書きます。
※2022年3月現在のエラーなので、今後のアップデートによりこのエラー自体が消える可能性があります。ご注意ください。

エラーの例

例として以下の、URLから写真を表示するコードを使います。

net_sample.dart
class NetSample extends StatelessWidget {
  const NetSample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            Container(
                width: 500,
                height: 500,
                child: Image.network("https://user-images.githubusercontent.com/89324742/157608435-2aaf7d31-6db4-4831-a1c6-3bf9fdae8070.JPG"))
          ],
        ),
      ),
    );
  }
}

これを

flutter run -d chrome

でChromeデバッグをすると
エラー画像1.png
このように画像が表示されません。ターミナルには

The following ImageCodecException was thrown resolving an image codec:
Failed to load network image.

とエラーメッセージが出ました。

対処法

以下のように実行してください。

flutter run -d chrome --web-renderer html

すると画像が正常に表示されます。
スクリーンショット 2022-03-10 165336.png
また、build webする時は、

flutter build web --web-renderer html --release

と実行してください。

おわりに

少しでも参考になれば幸いです!!

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2