💻
Gürkan Fikret Günak - Personal
  • 👨‍💻About me
    • 🌊Journey
  • 🎯Dart
    • 🔬What's Dart Algorithms?
    • 🔬What's Dart Structures?
    • 🧮#01 Algorithm Guidance: Implementing Calculation Algorithms
    • 🧮#02 Algorithm Guidance: Two Sum
  • 📄Guidances
    • Flutter MVVM Guidance
    • Dart Programming Guidance
    • E-Commerce Use Cases
    • E-Commerce Applications
    • Flutter App Color Palette Usage Guidance
    • Flutter Custom AppBar Usage Guidance
    • Flutter Network Image Cache Usage Guidance
    • Flutter Project Bitbucket SSH Guidance
    • Flutter Project GitHub SSH Guidance
    • Flutter SliverAppBar Usage Guidance
    • The Importance of BuildContext in Flutter Tests Guidance
    • Internship Basic Guidance v0.1.0
    • The Importance of Type Casting in Flutter
    • Effective and Detailed Pull Request Guide
    • Flutter Naming Conventions Guidance
    • Flutter Widget Guidance
    • Semantic Commit Guidance
    • Being Part of a Mobile Software Team and Working on a Shared Architecture
    • Understanding Deep Links for Any Development Platform
    • The Journey of a Developer: Stories of Becoming Junior, Middle, and Senior Developer
    • Becoming a Team Leader: Growing in Sync with Your Team
    • Why IP Changes Are Important for Mobile Applications in Flutter
    • Why Your Growing Mobile Team Needs CI/CD and How to Build a Winning Strategy
    • Dart in 2024: 20 Features You Need to Know With Code Examples and Scenarios
    • Remote Theme Management with API (JSON): Implementing a Helper in Flutter SDK
    • Understanding and Implementing Force Upgrade in Your Flutter Project
    • Life Lessons from the Bald Eagle: A Metaphor for Growth, Change, and Leadership
    • The Beauty of Imperfection: Why Today Doesn’t Need to Be Perfect
    • # The Reverse Curve of Productivity: When Social Cohesion in Software Teams Starts to Hurt **How str
    • 📱 Mobil Uygulamalarda GraphQL Tercihi: Bakım ve Maliyet Etkiler
    • 📉 Türkiye’de Yazılım Projelerinde Süreç Yönetimi ve Ekonomik Kayıp: Bir Bekâ Sorunu mu?
  • 📹VIDEOS
    • Introduction to Flutter Boilerplate! ( Turkish )
    • Flutter APIs effective using ( English )
    • Understand to SDK ( English )
  • Links
    • 💼 | Linkedin
    • 🆇 | x.com
    • 📧 | Mail me
Powered by GitBook
On this page
  • Description
  • Why Do We Face This Problem?
  • Potential Problems Caused by This Issue
  • Solution: Memory Management and Download Control
  • Scenario: Health Application
  • Summary
  • References
  1. Guidances

Flutter Network Image Cache Usage Guidance

Description

Flutter is an open-source UI framework that offers various UI components and tools to developers. Network Image Cache is an important concept that involves caching images fetched over the network to improve app performance and reduce network traffic.

Why Do We Face This Problem?

Fetching and displaying images from the network each time can negatively impact user experience and lead to unnecessary data usage. This can result in slow loading times, increased data consumption, and an inconsistent UI.

Potential Problems Caused by This Issue

  • Poor user experience due to slow image loading times.

  • Increased data consumption from repeatedly downloading the same images.

  • Degraded app performance due to constant network requests.

Solution: Memory Management and Download Control

class ImageCacheProvider extends InheritedWidget {
  final Map<String, Uint8List> imageCache = {};

  ImageCacheProvider({required Widget child}) : super(child: child);

  static ImageCacheProvider? of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<ImageCacheProvider>();
  }

  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) {
    return false;
  }
}

class CachedNetworkImageWidget extends StatelessWidget {
  final String imageUrl;

  CachedNetworkImageWidget({required this.imageUrl});

  @override
  Widget build(BuildContext context) {
    final imageCacheProvider = ImageCacheProvider.of(context);

    if (imageCacheProvider != null && imageCacheProvider.imageCache.containsKey(imageUrl)) {
      return Image.memory(
        imageCacheProvider.imageCache[imageUrl]!,
        fit: BoxFit.contain,
      );
    } else {
      _loadImage(context);
      return CircularProgressIndicator();
    }
  }

  Future<void> _loadImage(BuildContext context) async {
    final response = await http.get(Uri.parse(imageUrl));
    if (response.statusCode == 200) {
      final imageBytes = response.bodyBytes;

      final imageCacheProvider = ImageCacheProvider.of(context);
      if (imageCacheProvider != null) {
        imageCacheProvider.imageCache[imageUrl] = imageBytes;
      }

      context.dependOnInheritedWidgetOfExactType<ImageCacheProvider>()?.updateShouldNotify(covariant InheritedWidget oldWidget) => true;
    }
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ImageCacheProvider(
      child: MaterialApp(
        title: 'Cached Network Image Example',
        home: Scaffold(
          appBar: AppBar(
            title: Text('Cached Network Image'),
          ),
          body: Center(
            child: CachedNetworkImageWidget(imageUrl: "https://example.com/sample-image.jpg"),
          ),
        ),
      ),
    );
  }
}

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

This example demonstrates using InheritedWidget to create an image cache. It checks the cache to see if an image is already available, otherwise, it fetches and caches the image. The updateShouldNotify method ensures that the widget tree is updated when the cache changes.

Scenario: Health Application

Description: In this scenario, we'll address using Network Image Cache to manage a user's profile image in a health application.

Scenario Description: To prevent repeatedly downloading the user's profile image and to improve performance, we'll use Network Image Cache. The user's profile image will be cached for reuse across different screens and components.

Code Snippet:

class UserProfileImage extends StatelessWidget {
  final String imageUrl;

  UserProfileImage({required this.imageUrl});

  @override
  Widget build(BuildContext context) {
    final imageCacheProvider = ImageCacheProvider.of(context);

    if (imageCacheProvider != null && imageCacheProvider.imageCache.containsKey(imageUrl)) {
      return Image.memory(
        imageCacheProvider.imageCache[imageUrl]!,
        fit: BoxFit.contain,
      );
    } else {
      _loadImage(context);
      return CircularProgressIndicator();
    }
  }

  Future<void> _loadImage(BuildContext context) async {
    final response = await http.get(Uri.parse(imageUrl));
    if (response.statusCode == 200) {
      final imageBytes = response.bodyBytes;

      final imageCacheProvider = ImageCacheProvider.of(context);
      if (imageCacheProvider != null) {
        imageCacheProvider.imageCache[imageUrl] = imageBytes;
      }

      context.dependOnInheritedWidgetOfExactType<ImageCacheProvider>()?.updateShouldNotify(covariant InheritedWidget oldWidget) => true;
    }
  }
}

// Usage
UserProfileImage(imageUrl: "https://example.com/user-profile.png"),

Summary

Using Network Image Cache in Flutter is an effective way to improve app performance by caching images fetched from the network. In this Guidance, we learned how to use InheritedWidget to cache images and manage their retrieval. Additionally, we explored a scenario involving a health application where the user's profile image is cached for reuse.

References

PreviousFlutter Custom AppBar Usage GuidanceNextFlutter Project Bitbucket SSH Guidance

📄
Flutter Official Website
Dart Official Website
HTTP Package