crdt_sync_flutter 0.9.1: versionCode fallback

Without --dart-define=APP_BUILD (phone_deploy, local builds) error
reports carried build 0; the installed versionCode now fills it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-11-17 22:59:34 +01:00
parent d0cfc2ca1f
commit 48dc28ad97
6 changed files with 119 additions and 3 deletions

View File

@ -208,7 +208,8 @@ errors.connect(
`installAppErrors` chains `FlutterError.onError` and `PlatformDispatcher.onError`.
Reports are deduplicated on the device (30 s window, 20 a minute), redacted,
queued in `SharedPreferences` (so offline reports survive), and uploaded to
RTDB `/<app>/errors/<device>/<id>` with `repo`, `build` and `version`. A spinner
RTDB `/<app>/errors/<device>/<id>` with `repo`, `build` and `version`. Without
`APP_BUILD` the installed versionCode (`package_info_plus`) is the build. A spinner
inside `WaitWatch` that stays over 1 s becomes `perf.slow`. Nothing shows on
screen.

View File

@ -12,6 +12,7 @@ import 'package:crdt_sync_flutter/src/errors/error_upload.dart';
import 'package:crdt_sync_flutter/src/errors/slow_wait.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// The installed reporting: [reporter] for errors, [waits] for slow waits,
@ -54,14 +55,22 @@ class AppErrors {
/// `WidgetsFlutterBinding.ensureInitialized`: uncaught framework errors
/// (`uncaught.flutter`) and async errors (`uncaught.async`) are queued, then
/// handed to the handlers installed before. Shows nothing on screen.
///
/// A [config] with build 0 (no `--dart-define=APP_BUILD`) takes the installed
/// versionCode from [buildNumber] (default [platformBuildNumber]), so every
/// report names the build it came from; an unreadable one stays 0.
Future<AppErrors> installAppErrors(
AppErrorsConfig config, {
SharedPreferences? preferences,
Future<String?> Function() buildNumber = platformBuildNumber,
}) async {
final stamped = config.build != 0
? config
: config.withBuild(await _installedBuild(buildNumber));
final queue = await ErrorQueue.open(
preferences ?? await SharedPreferences.getInstance(),
);
final errors = AppErrors.instance = AppErrors.forTest(config, queue);
final errors = AppErrors.instance = AppErrors.forTest(stamped, queue);
final reporter = errors.reporter;
final framework = FlutterError.onError;
FlutterError.onError = (details) {
@ -83,6 +92,18 @@ Future<AppErrors> installAppErrors(
return errors;
}
/// The installed app's build number (Android versionCode).
Future<String?> platformBuildNumber() async =>
(await PackageInfo.fromPlatform()).buildNumber;
Future<int> _installedBuild(Future<String?> Function() read) async {
try {
return int.tryParse(await read() ?? '') ?? 0;
} on Object {
return 0;
}
}
/// Keeps the reporter's route current, so every report names its screen.
/// Add it to the app's `navigatorObservers`. Route names only: give routes
/// patterns (`/list/:id`), never ids or titles.

View File

@ -28,6 +28,15 @@ class AppErrorsConfig {
/// The version name (`1.2.3`).
final String? version;
/// This config with [build] replaced.
AppErrorsConfig withBuild(int build) => AppErrorsConfig(
app: app,
repo: repo,
package: package,
build: build,
version: version,
);
/// What marks the app's own stack frames (`package:todo/`).
String get mark => 'package:${package ?? app.replaceAll('-', '_')}/';
}

View File

@ -351,6 +351,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.0"
package_info_plus:
dependency: "direct main"
description:
name: package_info_plus
sha256: "73ef2cd5c11a7548f9b7f6fc2a4f6113de2e64aa2a70de63feede95548822908"
url: "https://pub.dev"
source: hosted
version: "10.2.2"
package_info_plus_platform_interface:
dependency: transitive
description:
name: package_info_plus_platform_interface
sha256: db762cb2f4f25ee60fb6359773861b0f199e00b90d237bd85a76a1e806b46ef4
url: "https://pub.dev"
source: hosted
version: "4.1.0"
path:
dependency: transitive
description:

View File

@ -4,7 +4,7 @@ description: >-
persisted per-install device id, and a one-call bootstrap. Removes the
~150 lines of firebase_backend.dart every consuming app was copying.
publish_to: 'none'
version: 0.9.0
version: 0.9.1
environment:
sdk: ^3.12.2
@ -36,6 +36,9 @@ dependencies:
# Flutter 3.50 deprecates package:flutter/material.dart. An app on
# material_ui gives flutter/material dialogs no MaterialLocalizations.
material_ui: 1.11.1
# The installed versionCode, stamped on error reports when the build did
# not pass --dart-define=APP_BUILD (phone_deploy and local builds).
package_info_plus: 10.2.2
shared_preferences: 2.5.6
# Opens the system browser for the desktop loopback sign-in.
url_launcher: 6.3.3

View File

@ -0,0 +1,66 @@
import 'dart:ui' show PlatformDispatcher;
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'app_errors_support.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() => SharedPreferences.setMockInitialValues({}));
group('build number fallback', () {
const bare = AppErrorsConfig(app: 'todo', repo: 'kuhyx/todo');
Future<AppErrors> install(
AppErrorsConfig c,
Future<String?> Function() buildNumber,
) async {
final saved = (FlutterError.onError, PlatformDispatcher.instance.onError);
final errors = await installAppErrors(
c,
preferences: await SharedPreferences.getInstance(),
buildNumber: buildNumber,
);
FlutterError.onError = saved.$1;
PlatformDispatcher.instance.onError = saved.$2;
AppErrors.instance = null;
return errors;
}
test('no APP_BUILD: the installed versionCode is used', () async {
final errors = await install(bare, () async => '173');
expect(errors.config.build, 173);
expect(errors.config.app, 'todo');
});
test('an explicit build wins over the installed one', () async {
final errors = await install(config, () async => '173');
expect(errors.config.build, 42);
});
test('an unreadable versionCode stays 0', () async {
expect((await install(bare, () async => 'x')).config.build, 0);
expect((await install(bare, () async => null)).config.build, 0);
expect(
(await install(bare, () async => throw StateError('no'))).config.build,
0,
);
});
});
test('the default versionCode comes from the platform', () async {
PackageInfo.setMockInitialValues(
appName: 'todo',
packageName: 'com.kuhy.todo',
version: '1.0.0',
buildNumber: '9',
buildSignature: '',
);
expect(await platformBuildNumber(), '9');
});
}