Check image is loaded in Image.network widget in flutter

后端 未结 5 798
忘了有多久
忘了有多久 2021-02-04 05:54

I am new to Flutter. I try to load network image using image.network widget. it\'s work fine but sometimes it\'s take time to load.I added tap listener to ima

5条回答
  •  清歌不尽
    2021-02-04 06:58

    for ones who do not need to cache the image can use meet_network_image package,

    The package basic usage :

                    MeetNetworkImage(
                      imageUrl:
                          "https://random.dog/3f62f2c1-e0cb-4077-8cd9-1ca76bfe98d5.jpg",
                      loadingBuilder: (context) => Center(
                            child: CircularProgressIndicator(),
                          ),
                      errorBuilder: (context, e) => Center(
                            child: Text('Error appear!'),
                          ),
                    )
    

    In addition, you can do that by yourself with using a FutureBuilder,

    We need to get data with http call that way, we need to import http before import you also need to add pubspec.yaml and run the command flutter packages get

    import 'package:http/http.dart' as http;
    
      FutureBuilder(
        // Paste your image URL inside the htt.get method as a parameter
        future: http.get(
            "https://random.dog/3f62f2c1-e0cb-4077-8cd9-1ca76bfe98d5.jpg"),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
              return Text('Press button to start.');
            case ConnectionState.active:
            case ConnectionState.waiting:
              return CircularProgressIndicator();
            case ConnectionState.done:
              if (snapshot.hasError)
                return Text('Error: ${snapshot.error}');
              // when we get the data from the http call, we give the bodyBytes to Image.memory for showing the image
              return Image.memory(snapshot.data.bodyBytes);
          }
          return null; // unreachable
        },
      );
    

提交回复
热议问题