flutter_persistent_queue 0.1.4 copy "flutter_persistent_queue: ^0.1.4" to clipboard
flutter_persistent_queue: ^0.1.4 copied to clipboard

outdated

Simple file-based non-volatile persistent queue implementation for flutter. Ideal for in-device sequential buffers that must persist between app runs.

example/lib/main.dart

import 'dart:math' show Random;
import 'package:flutter/foundation.dart' show debugPrint;
import 'package:flutter/material.dart';
import 'package:flutter_persistent_queue/flutter_persistent_queue.dart';

void main() => runApp(_MyApp());

class _MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _runTests(),
      builder: (context, snapshot) {
        final txt = snapshot.data == null ? 'Wait!' : snapshot.data as String;
        return MaterialApp(
          home: Scaffold(
            body: Center(
              child: Text(txt),
            ),
          ),
        );
      },
    );
  }
}

Future<String> _runTests() async {
  try {
    await _basicTest();
    return 'Queue works! 😀';
  } catch (e, s) {
    _errHdlr(e, s);
    return 'Something went wrong 😤';
  }
}

Future<void> _basicTest() async {
  final pq = PersistentQueue(filename: 'pq', flushAt: 2000, errFunc: _errHdlr);
  await pq.setup(noReload: true);
  print('queue ready? ${pq.ready ? 'YES' : 'NO'}');

  final Set<int> sourceSet = Set(), targetSet = Set();
  for (int i = 1000; i > 0; --i) {
    final int val = Random().nextInt(4294967296);
    sourceSet.add(val);
    await pq.push(<String, dynamic>{'val': val});
  }
  print('queue loaded, ${pq.length} elements');

  await pq.flush((list) async {
    targetSet.addAll(list.map((val) => val['val'] as int));
  });
  print('queue flushed');

  final sourceList = sourceSet.toList(), targetList = targetSet.toList();
  sourceList.sort();
  targetList.sort();

  _assert(sourceList.length == targetList.length);
  for (int i = sourceList.length - 1; i >= 0; --i) {
    _assert(sourceList[i] == targetList[i]);
  }
}

void _assert(bool cta) => cta != true ? throw Exception('QueueFailed') : null;
void _errHdlr(dynamic err, [StackTrace stack]) => debugPrint('$err\n$stack');
10
likes
0
pub points
17%
popularity

Publisher

unverified uploader

Simple file-based non-volatile persistent queue implementation for flutter. Ideal for in-device sequential buffers that must persist between app runs.

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, localstorage, meta, synchronized

More

Packages that depend on flutter_persistent_queue