Storage on every platform: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: text (line 40), remove (line 46), binary (line 49).
1/// Small documents a player's choices live in, kept the right way on
2/// whichever of five platforms is running: a file on four of them, an
3/// IndexedDB record for anything too big for `localStorage` on the fifth.
4///
5/// Quoted by `storage.md` and shown whole in the Source tab.
6library;
8import 'dart:typed_data';
10import 'package:flutter/widgets.dart';
11import 'package:flutter3d/flutter3d.dart';
12import 'package:flutter3d_app/flutter3d_app.dart';
13import 'package:flutter3d_showcase/src/demo/demo.dart';
15/// The `storage` page: documents and bytes kept through `Storage`. Its check
16/// is that both read back exactly and that a removed document reads back as
17/// nothing.
18final class StorageDemo extends ShowcaseDemo {
19 late final String _report;
22 Scene build(DemoContext context) {
23 final material = RenderMaterial(
24 name: 'disk',
25 baseColor: LinearColor.fromSrgb(0.6, 0.6, 0.7, 1.0),
26 );
27 final node = MeshNode(
28 DeviceMesh.upload(context.device, SphereShape(segments: 16).build()),
29 material,
30 );
31 return Scene()
32 ..add(node)
33 ..add(
34 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
35 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
36 );
37 }
39 static Future<String> _run() async {
40 final storage = defaultStorage('flutter3d-showcase-demo');
41 final wrote = await storage
42 .write('settings.json', '{"volume":0.8}')
43 .then((_) => true, onError: (Object _) => false);
44 final readBack = await storage.read('settings.json');
46 await storage.remove('settings.json');
47 final afterRemove = await storage.read('settings.json');
49 final binary = defaultBinaryStorage('flutter3d-showcase-demo');
50 final bytes = Uint8List.fromList(<int>[1, 2, 3, 4]);
51 await binary.write('blob.bin', bytes);
52 final readBytes = await binary.read('blob.bin');
53 await binary.remove('blob.bin');
55 return 'wrote: $wrote, read back: $readBack, after remove: $afterRemove\n'
56 'binary round trip: $readBytes';
57 }
60 Future<void> prepare(DemoContext context) async {
61 _report = await _run();
62 }
65 Widget? customBody(BuildContext buildContext, DemoContext context) =>
66 Container(
67 color: const Color(0xFF14161A),
68 padding: const EdgeInsets.all(24),
69 alignment: Alignment.topLeft,
70 child: DefaultTextStyle(
71 style: const TextStyle(color: Color(0xFFE8E8EC), fontSize: 16),
72 child: Text(_report),
73 ),
74 );
77 void verify(Scene scene, FrameResult frame) {
78 if (frame.drawCalls < 1) {
79 throw StateError('the disk marker was not drawn');
80 }
81 if (!_report.contains('read back: {"volume":0.8}')) {
82 throw StateError('what was written should read back exactly');
83 }
84 if (!_report.contains('after remove: null')) {
85 throw StateError('a removed document should read back as nothing');
86 }
87 if (!_report.contains('binary round trip: [1, 2, 3, 4]')) {
88 throw StateError('the bytes written should read back exactly');
89 }
90 }
91}