flutter_hotpatch 0.1.1
flutter_hotpatch: ^0.1.1 copied to clipboard
Signed over-the-air Dart code patches for Flutter apps on Android, with staged rollouts and automatic rollback of patches that fail to boot.
example/lib/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_hotpatch/flutter_hotpatch.dart';
import 'message.dart';
/// Demo settings come from `--dart-define`, so the same code works with any
/// patch store and key. Real apps would hard-code these.
const _config = HotpatchConfig(
baseUrl: String.fromEnvironment(
'HOTPATCH_URL',
defaultValue: 'http://127.0.0.1:8787',
),
appId: 'io.github.vsiva253.hotpatch_example',
publicKeys: {
String.fromEnvironment('HOTPATCH_KEY_ID'): String.fromEnvironment(
'HOTPATCH_PUBLIC_KEY',
),
},
// Demo only: the device reaches a local HTTP server through `adb reverse`.
allowInsecureHttp: true,
);
void main() {
// Lets the demo publish a patch that dies before its first frame. The
// runtime check keeps the compiler from dropping the UI code (and with it
// the tree-shaken icon font) when kCrashOnStart is true.
if (kCrashOnStart && Platform.numberOfProcessors > 0) exit(3);
runApp(const DemoApp());
}
class DemoApp extends StatefulWidget {
const DemoApp({super.key});
@override
State<DemoApp> createState() => _DemoAppState();
}
class _DemoAppState extends State<DemoApp> {
HotpatchStatus? _status;
String _lastCheck = 'not checked yet';
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _refresh(check: true));
}
Future<void> _refresh({required bool check}) async {
final status = await Hotpatch.status();
if (!mounted) return;
setState(() => _status = status);
// The end-to-end script reads these lines from logcat.
// ignore: avoid_print
print(
'HOTPATCH_DEMO message="$kMessage" running=$kHotpatchBuildPatch '
'booted=${status?.bootedPatch} rolledBack=${status?.rolledBackPatch} '
'next=${status?.nextPatch} bad=${status?.badPatches}',
);
if (!check) return;
final result = await Hotpatch.checkForUpdate(_config);
final after = await Hotpatch.status();
if (!mounted) return;
setState(() {
_lastCheck = '$result';
_status = after;
});
// ignore: avoid_print
print(
'HOTPATCH_DEMO check=${result.outcome.name} patch=${result.patchNumber} '
'next=${after?.nextPatch} detail=${result.message}',
);
}
@override
Widget build(BuildContext context) {
final status = _status;
return MaterialApp(
title: 'hotpatch demo',
home: Scaffold(
appBar: AppBar(title: const Text('hotpatch demo')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
kMessage,
key: const Key('message'),
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 16),
Text('Running patch: $kHotpatchBuildPatch'),
Text('Booted patch: ${status?.bootedPatch ?? '-'}'),
Text('Next launch: ${status?.nextPatch ?? '-'}'),
Text('Rolled back: ${status?.rolledBackPatch ?? '-'}'),
Text('Last check: $_lastCheck'),
const SizedBox(height: 16),
FilledButton(
onPressed: () => _refresh(check: true),
child: const Text('Check for update'),
),
],
),
),
),
);
}
}