native_update 1.0.1
native_update: ^1.0.1 copied to clipboard
Signed over-the-air asset updates, Play in-app update and in-app review for Flutter apps, on Native Update.
example/lib/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_update/native_update.dart';
// Server and keys come from --dart-define; the defaults reach a local test server through
// `adb reverse tcp:8765 tcp:8765` (plain http is accepted only by debug builds).
const _serverUrl = String.fromEnvironment(
'NU_SERVER_URL',
defaultValue: 'http://127.0.0.1:8765/api',
);
const _apiKey = String.fromEnvironment(
'NU_API_KEY',
defaultValue: 'nu_app_local_test',
);
const _publicKey = String.fromEnvironment('NU_PUBLIC_KEY');
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await NativeUpdate.init(
const NativeUpdateConfig(
serverUrl: _serverUrl,
apiKey: _apiKey,
publicKey: _publicKey,
),
);
runApp(const _TestApp());
}
class _TestApp extends StatelessWidget {
const _TestApp();
@override
Widget build(BuildContext context) => const MaterialApp(home: _StatusPage());
}
/// Shows which marker file is in use and runs one check-and-download per start. A bundle whose behaviour.txt
/// says skip-ready never calls notifyAppReady(), which stands in for a crash in the rollback test.
class _StatusPage extends StatefulWidget {
const _StatusPage();
@override
State<_StatusPage> createState() => _StatusPageState();
}
class _StatusPageState extends State<_StatusPage> {
final _lines = <String>[];
@override
void initState() {
super.initState();
_start();
}
Future<void> _start() async {
final markerPath = await NativeUpdate.file('marker.txt');
final marker = markerPath != null
? File(markerPath).readAsStringSync()
: await rootBundle.loadString('assets/marker.txt');
final behaviourPath = await NativeUpdate.file('behaviour.txt');
final ready =
behaviourPath == null ||
File(behaviourPath).readAsStringSync().trim() != 'skip-ready';
if (ready) await NativeUpdate.notifyAppReady();
_show(
'marker=$marker version=${await NativeUpdate.currentVersion()} ready=$ready '
'dropped=${await NativeUpdate.lastDroppedReason()}',
);
await _checkAndDownload();
}
Future<void> _checkAndDownload() async {
try {
final result = await NativeUpdate.checkForUpdate();
switch (result) {
case UpdateAvailable(:final info):
_show(
'downloaded ${await NativeUpdate.download(info)}, used from next start',
);
case UpToDate(:final message, :final code):
_show('check: up to date ($code) $message');
case NativeUpdateRequired(:final minNativeVersion):
_show('check: needs app $minNativeVersion');
case UpdateHeld(:final message):
_show('check: held $message');
}
} on NativeUpdateException catch (error) {
_show('error ${error.code}: ${error.message}');
}
}
void _show(String line) {
debugPrint('NUFlutter $line');
if (mounted) setState(() => _lines.add(line));
}
@override
Widget build(BuildContext context) => Scaffold(
body: SafeArea(
child: ListView(
padding: const EdgeInsets.all(24),
children: [
for (final line in _lines)
Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Text(line),
),
FilledButton(
onPressed: _checkAndDownload,
child: const Text('Check now'),
),
],
),
),
);
}