add project
This commit is contained in:
69
example/lib/dio.dart
Normal file
69
example/lib/dio.dart
Normal file
@@ -0,0 +1,69 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:native_updater/native_updater.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'native_updater DIO example',
|
||||
home: Home(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Home extends StatefulWidget {
|
||||
@override
|
||||
_HomeState createState() => _HomeState();
|
||||
}
|
||||
|
||||
class _HomeState extends State<Home> {
|
||||
Future<void> checkVersion(int statusCode) async {
|
||||
/// For example: You got status code of 412 from the
|
||||
/// response of HTTP request.
|
||||
/// Let's say the statusCode 412 requires you to force update
|
||||
Future.delayed(Duration.zero, () {
|
||||
if (statusCode == HttpStatus.unauthorized) {
|
||||
NativeUpdater.displayUpdateAlert(
|
||||
context,
|
||||
forceUpdate: true,
|
||||
appStoreUrl: '<Your App Store URL>',
|
||||
iOSDescription: '<Your iOS description>',
|
||||
iOSUpdateButtonLabel: 'Upgrade',
|
||||
iOSCloseButtonLabel: 'Exit',
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Your App'),
|
||||
),
|
||||
body: Center(
|
||||
child: TextButton(
|
||||
onPressed: requestAPI,
|
||||
child: Text('Request API')
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
requestAPI() async {
|
||||
var dio = Dio(BaseOptions(
|
||||
baseUrl: 'http://httpbin.org/',
|
||||
));
|
||||
|
||||
try {
|
||||
Response response = await dio.get('/get');
|
||||
} on DioError catch (e) {
|
||||
checkVersion(e.response.statusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
77
example/lib/main.dart
Normal file
77
example/lib/main.dart
Normal file
@@ -0,0 +1,77 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:native_updater/native_updater.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'native_updater example',
|
||||
home: Home(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Home extends StatefulWidget {
|
||||
@override
|
||||
_HomeState createState() => _HomeState();
|
||||
}
|
||||
|
||||
class _HomeState extends State<Home> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
checkVersion();
|
||||
}
|
||||
|
||||
Future<void> checkVersion() async {
|
||||
/// For example: You got status code of 412 from the
|
||||
/// response of HTTP request.
|
||||
/// Let's say the statusCode 412 requires you to force update
|
||||
int statusCode = 412;
|
||||
|
||||
/// This could be kept in our local
|
||||
int localVersion = 9;
|
||||
|
||||
/// This could get from the API
|
||||
int serverLatestVersion = 10;
|
||||
|
||||
Future.delayed(Duration.zero, () {
|
||||
if (statusCode == 412) {
|
||||
NativeUpdater.displayUpdateAlert(
|
||||
context,
|
||||
forceUpdate: true,
|
||||
appStoreUrl: '<Your App Store URL>',
|
||||
iOSDescription: '<Your iOS description>',
|
||||
iOSUpdateButtonLabel: 'Upgrade',
|
||||
iOSCloseButtonLabel: 'Exit',
|
||||
errorText: "Error",
|
||||
errorCloseButtonLabel: "Close",
|
||||
errorSubtitle: "This version of the app isn't legit"
|
||||
);
|
||||
} else if (serverLatestVersion > localVersion) {
|
||||
NativeUpdater.displayUpdateAlert(
|
||||
context,
|
||||
forceUpdate: false,
|
||||
appStoreUrl: '<Your App Store URL>',
|
||||
iOSDescription: '<Your description>',
|
||||
iOSUpdateButtonLabel: 'Upgrade',
|
||||
iOSIgnoreButtonLabel: 'Next Time',
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Your App'),
|
||||
),
|
||||
body: Center(
|
||||
child: Text('Testing...'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user