Baseflow / flutter_cached_network_image

Download, cache and show images in a flutter app
https://baseflow.com
2.44k stars 656 forks source link

Question: i want to display cache images from api data response's images how can i do it? simple online images url works perfectly but when work with api images it not display image from cache when data is off #889

Open mini0555555 opened 11 months ago

mini0555555 commented 11 months ago

🔙 Regression

Old (and correct) behavior

Current behavior

Reproduction steps

Configuration

Version: 1.x

Platform:

renefloor commented 11 months ago

Can you explain a bit more what you're trying to achieve and if you can, give an example? I don't understand what you're asking. I would expect you get an image url from your api?

mini0555555 commented 11 months ago

import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; import 'package:http/http.dart' as http; import 'package:signinwithmobileno/utility/api_urls.dart'; import 'modal/banner_modal.dart';

class StaggeredGridViewPage extends StatefulWidget { const StaggeredGridViewPage({Key? key}) : super(key: key);

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

class _StaggeredGridViewPageState extends State { List imageList = [ 'https://cdn.pixabay.com/photo/2019/03/15/09/49/girl-4056684_960_720.jpg', 'https://cdn.pixabay.com/photo/2020/12/15/16/25/clock-5834193__340.jpg', 'https://cdn.pixabay.com/photo/2020/09/18/19/31/laptop-5582775_960_720.jpg', 'https://media.istockphoto.com/photos/woman-kayaking-in-fjord-in-norway-picture-id1059380230?b=1&k=6&m=1059380230&s=170667a&w=0&h=kA_A_XrhZJjw2bo5jIJ7089-VktFK0h0I4OWDqaac0c=', 'https://cdn.pixabay.com/photo/2019/11/05/00/53/cellular-4602489_960_720.jpg', 'https://cdn.pixabay.com/photo/2017/02/12/10/29/christmas-2059698_960_720.jpg', 'https://cdn.pixabay.com/photo/2020/01/29/17/09/snowboard-4803050_960_720.jpg', 'https://cdn.pixabay.com/photo/2020/02/06/20/01/university-library-4825366_960_720.jpg', 'https://cdn.pixabay.com/photo/2020/11/22/17/28/cat-5767334_960_720.jpg', 'https://cdn.pixabay.com/photo/2020/12/13/16/22/snow-5828736_960_720.jpg', 'https://cdn.pixabay.com/photo/2020/12/13/16/22/snow-5828736_960_720.jpg', ]; List bannerlist = []; BannerResponse? bannerResponse; var fetchedFile; @override void initState() { super.initState(); fetchBannerImages(); }

fetchBannerImages() async { try { final Map<String, String> formData = { 'company_id': '2', }; final response = await http.post( Uri.parse(ApiUrls.bannerlist), body: formData, );

  if (response.statusCode == 200) {
    bannerResponse = BannerResponse.fromJson(json.decode(response.body));
    bannerlist.addAll(bannerResponse!.result as List<Baner>);

    print(response.body);
  }
} catch (e) {
  print('Error: $e');
}

}

@override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: MasonryGridView.builder( itemCount: imageList.length, gridDelegate: SliverSimpleGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, ), itemBuilder: (BuildContext context, int index) { return CachedNetworkImage( imageUrl: imageList[index],); // it display perfectly // return CachedNetworkImage(imageUrl: bannerlist[index].bannerImage!); // it not display images }, ), ), ); } }

here is my code in which when i display static url's of image list then it display images even if i am offline but when i display url's of bannerlist which is not static url's but it get from http request then it don't display images when i am offline . so all i want is if my phone is in offline mode still it display images.