dynamic_cache 0.0.1 copy "dynamic_cache: ^0.0.1" to clipboard
dynamic_cache: ^0.0.1 copied to clipboard

outdated

A package for create a complete in-memory cache in Flutter. Inspired by IMemoryCache from Microsoft.Extensions.Caching.Memory .

example/lib/main.dart

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

void main() {
  runApp(
    /// Wrap your app with DynamicCacheProvider
    /// Note: specifying the cache is optional, as it will
    /// create one if it does not receive the parameter.
    const DynamicCacheProvider(
      cache: DynamicCache(),
      child: MyApp(),
    ),
  );
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        useMaterial3: true,
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

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

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  /// Get a [DynamicCache] global instance to use the cache.
  final _cache = DynamicCache.instance;

  /// Second possible method will be commented below:
  // bool _initialized = false;
  // @override
  // void didChangeDependencies() {
  //   if (!_initialized) {
  //     context.cache.create('counter', 0, autoRemove: false, listen: false);
  //     _initialized = true;
  //   }
  //   super.didChangeDependencies();
  // }

  @override
  void initState() {
    /// If using in a widget , initialize the cache first in
    /// initState if the cache is empty before creating widgets that
    /// explicitly depend on this data.
    /// Note: set listen to false to not call the setState method in the build.
    _cache.create('counter', 0, autoRemove: false, listen: false);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Dynamic Cache Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              context.cache.get<int>('counter').toString(),
              style: const TextStyle(fontSize: 24),
            ),
          ],
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          context.cache.update<int>('counter', (value) => value + 1);
        },
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
3
likes
0
points
121
downloads

Publisher

verified publisherworkstationti.host

Weekly Downloads

A package for create a complete in-memory cache in Flutter. Inspired by IMemoryCache from Microsoft.Extensions.Caching.Memory .

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on dynamic_cache