Mahesh
Mahesh

Reputation: 23

Image.network stops showing images

Sample code to test

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

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

class MyApp extends StatelessWidget {
// This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
  StreamController controller;
  List imgs = [
    'https://images.unsplash.com/photo-1519336367661-eba9c1dfa5e9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80',
    'https://images.unsplash.com/photo-1594164070019-a3bd58576d55?ixlib=rb-1.2.1&auto=format&fit=crop&w=675&q=80',
    'http://www.example.com'
  ];
  int i = 0;
  @override
  void initState() {
    super.initState();

    controller = StreamController();
    const oneSec = const Duration(seconds: 5);
    new Timer.periodic(oneSec, (Timer t) {
      print('value of i $i');
      controller.sink.add(imgs[i]);
      i++;
      if (i > 2) {
        i = 0;
      }
    });
  }

  @override
  void dispose() {
    controller.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: controller.stream,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Image.network(
              snapshot.data,
              loadingBuilder: (context, child, loading) {
                if (loading == null) return Center(child: child);
                return Center(child: CircularProgressIndicator());
              },
              errorBuilder: (context, object, error) {
                return Center(child: CircularProgressIndicator());
              },
            );
          } else {
            return Container();
          }
        },
      ),
    );
  }
}

The third image is not displayed. It is obvious. But after errorBuilder, The code does not show any other valid network images.

In github, i said it is a bug.
But the team said i must ask the question in stackoverflow

Is it a bug or am i making any mistake?

Upvotes: 0

Views: 210

Answers (1)

yellowgray
yellowgray

Reputation: 4509

I think you can add key: UniqueKey(), inside Image.network. Flutter always try to reuse most of the widget to avoid rendering cost (maybe it keep the error status). Add UniqueKey to force rebuild.

Upvotes: 1

Related Questions