0% found this document useful (0 votes)
23 views1 page

Consumable Store - Dart

This document defines a ConsumableStore class that uses SharedPreferences to locally store consumable IDs. The class provides methods to save, consume, and load consumable IDs from SharedPreferences.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views1 page

Consumable Store - Dart

This document defines a ConsumableStore class that uses SharedPreferences to locally store consumable IDs. The class provides methods to save, consume, and load consumable IDs from SharedPreferences.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

// Copyright 2019 The Chromium Authors. All rights reserved.

// Use of this source code is governed by a BSD-style license that can be


// found in the LICENSE file.

import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';

// This is just a development prototype for locally storing consumables. Do not


// use this.
class ConsumableStore {
static const String _kPrefKey = 'consumables';
static Future<void> _writes = Future.value();

static Future<void> save(String id) {


_writes = _writes.then((void _) => _doSave(id));
return _writes;
}

static Future<void> consume(String id) {


_writes = _writes.then((void _) => _doConsume(id));
return _writes;
}

static Future<List<String>> load() async {


return (await SharedPreferences.getInstance()).getStringList(_kPrefKey) ??
[];
}

static Future<void> _doSave(String id) async {


List<String> cached = await load();
SharedPreferences prefs = await SharedPreferences.getInstance();
cached.add(id);
await prefs.setStringList(_kPrefKey, cached);
}

static Future<void> _doConsume(String id) async {


List<String> cached = await load();
SharedPreferences prefs = await SharedPreferences.getInstance();
cached.remove(id);
await prefs.setStringList(_kPrefKey, cached);
}
}

You might also like