web_cache_clear 0.0.3 copy "web_cache_clear: ^0.0.3" to clipboard
web_cache_clear: ^0.0.3 copied to clipboard

Platformweb

A Flutter web plugin to automatically clear the browser cache and reload the page when a new app version is detected. Ideal for ensuring users get the latest version.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:web/web.dart' as html;

import 'package:flutter/services.dart';
import 'package:web_cache_clear/web_cache_clear.dart';
import 'package:web_cache_clear/web_cache_clear_current_version.dart';

void main() {
  // Ensure that plugin services are initialized so that `available` works
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _cacheStatus = 'Cache status: Unknown';
  final _webCacheClearPlugin = WebCacheClear();

  @override
  void initState() {
    super.initState();
    _checkVersionFromServer();
  }

  Future<void> _checkVersionFromServer() async {
    // --- Placeholder for your server fetch ---
    // Replace this with your actual logic to fetch the version string.
    // For example, using the `http` package.
    final String serverVersion = await Future.delayed(
      const Duration(milliseconds: 100),
      () => "1.0.3",
    );
    // -----------------------------------------

    // This single line checks the version and reloads if they don't match.
    await _webCacheClearPlugin.checkAppVersion(serverVersion);

    // This code only runs if the versions match and the page doesn't reload.
    if (mounted) {
      setState(() {
        final storedVersion = html.window.sessionStorage.getItem(getCacheKey());
        _cacheStatus = 'Cache contains version: $storedVersion';
      });
    }
  }

  Future<void> _clearWebCache() async {
    await _webCacheClearPlugin.clearCache();
    // After clearing, update the UI to show the cache is empty (null).
    setState(() {
      _cacheStatus = 'Cache contains version: null';
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Plugin example app')),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(_cacheStatus),
                const SizedBox(height: 16),
                ElevatedButton(
                  onPressed: _clearWebCache,
                  child: const Text('Clear Web Cache'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
0
likes
160
points
175
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter web plugin to automatically clear the browser cache and reload the page when a new app version is detected. Ideal for ensuring users get the latest version.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_web_plugins, plugin_platform_interface, web

More

Packages that depend on web_cache_clear

Packages that implement web_cache_clear