flutter: Replace sentry by catcher and remove all try...catch...

This commit is contained in:
Calcitem 2021-02-16 17:39:04 +08:00
parent 96b2dd7533
commit c984771662
7 changed files with 101 additions and 128 deletions

View File

@ -44,13 +44,8 @@ class Profile {
Future<bool> commit() async { Future<bool> commit() async {
_file.create(recursive: true); _file.create(recursive: true);
try {
final contents = jsonEncode(_values); final contents = jsonEncode(_values);
await _file.writeAsString(contents); await _file.writeAsString(contents);
} catch (e) {
print('Error: $e');
return false;
}
return true; return true;
} }
@ -59,12 +54,8 @@ class Profile {
final docDir = await getApplicationDocumentsDirectory(); final docDir = await getApplicationDocumentsDirectory();
_file = File('${docDir.path}/$fileName'); _file = File('${docDir.path}/$fileName');
try {
final contents = await _file.readAsString(); final contents = await _file.readAsString();
_values = jsonDecode(contents); _values = jsonDecode(contents);
} catch (e) {
return false;
}
return true; return true;
} }

View File

@ -27,60 +27,29 @@ class NativeEngine extends AiEngine {
static const platform = const MethodChannel('com.calcitem.sanmill/engine'); static const platform = const MethodChannel('com.calcitem.sanmill/engine');
Future<void> startup() async { Future<void> startup() async {
try {
await platform.invokeMethod('startup'); await platform.invokeMethod('startup');
} catch (e) {
print('Native startup Error: $e');
}
await waitResponse(['uciok'], sleep: 1, times: 30); await waitResponse(['uciok'], sleep: 1, times: 30);
} }
Future<void> send(String command) async { Future<void> send(String command) async {
try {
print("send: $command"); print("send: $command");
await platform.invokeMethod('send', command); await platform.invokeMethod('send', command);
} catch (e) {
print('Native sendCommand Error: $e');
}
} }
Future<String> read() async { Future<String> read() async {
try {
return await platform.invokeMethod('read'); return await platform.invokeMethod('read');
} catch (e) {
print('Native readResponse Error: $e');
}
return null;
} }
Future<void> shutdown() async { Future<void> shutdown() async {
try {
await platform.invokeMethod('shutdown'); await platform.invokeMethod('shutdown');
} catch (e) {
print('Native shutdown Error: $e');
}
} }
Future<bool> isReady() async { Future<bool> isReady() async {
try {
return await platform.invokeMethod('isReady'); return await platform.invokeMethod('isReady');
} catch (e) {
print('Native readResponse Error: $e');
}
return null;
} }
Future<bool> isThinking() async { Future<bool> isThinking() async {
try {
return await platform.invokeMethod('isThinking'); return await platform.invokeMethod('isThinking');
} catch (e) {
print('Native readResponse Error: $e');
}
return null;
} }
@override @override

View File

@ -18,29 +18,62 @@
import 'dart:io'; import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:catcher/catcher.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sanmill/generated/l10n.dart'; import 'package:sanmill/generated/l10n.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'services/audios.dart'; import 'services/audios.dart';
import 'services/player.dart'; import 'services/player.dart';
import 'widgets/main_menu.dart'; import 'widgets/main_menu.dart';
Future<void> main() async { Future<void> main() async {
if (kReleaseMode) { var catcher = Catcher(rootWidget: SanmillApp(), ensureInitialized: true);
await SentryFlutter.init(
(options) { //DateTime now = DateTime.now();
options.dsn = //String formattedDate = DateFormat('yyyy-MM-dd_kk-mm').format(now);
'https://62c565096ba146a6b70bc57dbb72386c@o525088.ingest.sentry.io/5638585'; Directory externalDir = await getExternalStorageDirectory();
}, String path = externalDir.path.toString() + "/sanmill-crash-logs.txt";
appRunner: () => runApp(SanmillApp()), print("ExternalStorageDirectory: " + externalDir.path.toString());
String recipients = "calcitem@outlook.com";
/// Create catcher configuration.
/// Debug configuration with dialog report mode and console handler.
/// It will show dialog and once user accepts it, error will be shown
/// in console.
//CatcherOptions debugOptions = CatcherOptions(
// PageReportMode(showStackTrace: true), [ConsoleHandler()]);
CatcherOptions debugOptions =
CatcherOptions(PageReportMode(showStackTrace: true), [
ConsoleHandler(),
FileHandler(File(path), printLogs: true),
EmailManualHandler([recipients], printLogs: true)
]);
/// Release configuration.
/// Same as above, but once user accepts dialog,
/// user will be prompted to send email with crash to support.
CatcherOptions releaseOptions =
CatcherOptions(PageReportMode(showStackTrace: true), [
FileHandler(File(path), printLogs: true),
EmailManualHandler([recipients], printLogs: true)
]);
CatcherOptions profileOptions =
CatcherOptions(PageReportMode(showStackTrace: true), [
ConsoleHandler(),
FileHandler(File(path), printLogs: true),
EmailManualHandler([recipients], printLogs: true)
]);
/// Pass root widget (MyApp) along with Catcher configuration:
catcher.updateConfig(
debugConfig: debugOptions,
releaseConfig: releaseOptions,
profileConfig: profileOptions,
); );
} else {
runApp(SanmillApp());
}
SystemChrome.setPreferredOrientations( SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown], [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
@ -70,14 +103,15 @@ class _SanmillAppState extends State<SanmillApp> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
//Audios.loopBgm('bg_music.mp3');
Player.loadProfile(); Player.loadProfile();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
//
return MaterialApp( return MaterialApp(
/// Add navigator key from Catcher.
/// It will be used to navigate user to report page or to show dialog.
navigatorKey: Catcher.navigatorKey,
navigatorObservers: [routeObserver], navigatorObservers: [routeObserver],
localizationsDelegates: [ localizationsDelegates: [
// ... app-specific localization delegate[s] here // ... app-specific localization delegate[s] here

View File

@ -26,19 +26,15 @@ class Audios {
static AudioCache _bgmPlayer, _tonePlayer; static AudioCache _bgmPlayer, _tonePlayer;
static loopBgm(String fileName) async { static loopBgm(String fileName) async {
//
try {
if (_bgmPlayer == null) { if (_bgmPlayer == null) {
_fixedBgmPlayer = AudioPlayer(); _fixedBgmPlayer = AudioPlayer();
_bgmPlayer = _bgmPlayer = AudioCache(prefix: 'audios/', fixedPlayer: _fixedBgmPlayer);
AudioCache(prefix: 'audios/', fixedPlayer: _fixedBgmPlayer);
//await _bgmPlayer.loadAll(['bg_music.mp3']); //await _bgmPlayer.loadAll(['bg_music.mp3']);
} }
_fixedBgmPlayer.stop(); _fixedBgmPlayer.stop();
_bgmPlayer.loop(fileName); _bgmPlayer.loop(fileName);
} catch (e) {}
} }
static playTone(String fileName) async { static playTone(String fileName) async {
@ -46,7 +42,6 @@ class Audios {
return; return;
} }
try {
if (_tonePlayer == null) { if (_tonePlayer == null) {
// //
_fixedTonePlayer = AudioPlayer(); _fixedTonePlayer = AudioPlayer();
@ -72,23 +67,18 @@ class Audios {
await _fixedTonePlayer.seek(Duration.zero); await _fixedTonePlayer.seek(Duration.zero);
//await release(); //await release();
await _tonePlayer.play(fileName); await _tonePlayer.play(fileName);
} catch (e) {}
} }
static stopBgm() { static stopBgm() {
try {
if (_fixedBgmPlayer != null) _fixedBgmPlayer.stop(); if (_fixedBgmPlayer != null) _fixedBgmPlayer.stop();
} catch (e) {}
} }
static Future<void> release() async { static Future<void> release() async {
try {
if (_fixedBgmPlayer != null) { if (_fixedBgmPlayer != null) {
await _fixedBgmPlayer.release(); await _fixedBgmPlayer.release();
} }
if (_fixedTonePlayer != null) { if (_fixedTonePlayer != null) {
await _fixedTonePlayer.release(); await _fixedTonePlayer.release();
} }
} catch (e) {}
} }
} }

View File

@ -285,13 +285,6 @@ class _GamePageState extends State<GamePage> with RouteAware {
msg: S.of(context).analyzing, position: ToastPostion.bottom); msg: S.of(context).analyzing, position: ToastPostion.bottom);
setState(() => _searching = true); setState(() => _searching = true);
try {} catch (e) {
Toast.toast(context,
msg: S.of(context).error + ": $e", position: ToastPostion.bottom);
} finally {
setState(() => _searching = false);
}
} }
showAnalyzeItems( showAnalyzeItems(

View File

@ -58,17 +58,13 @@ class _MainMenuState extends State<MainMenu> with TickerProviderStateMixin {
if (status == AnimationStatus.completed) shadowController.reverse(); if (status == AnimationStatus.completed) shadowController.reverse();
}); });
/// use 'try...catch' to avoid exception - /// TODO: use 'try...catch' to avoid exception -
/// 'setState() or markNeedsBuild() called during build.' /// 'setState() or markNeedsBuild() called during build.'
inAnimation.addListener(() { inAnimation.addListener(() {
try {
setState(() {}); setState(() {});
} catch (e) {}
}); });
shadowAnimation.addListener(() { shadowAnimation.addListener(() {
try {
setState(() {}); setState(() {});
} catch (e) {}
}); });
inController.forward(); inController.forward();

View File

@ -19,7 +19,7 @@ dependencies:
url_launcher: ^5.7.10 url_launcher: ^5.7.10
intl: ^0.16.1 intl: ^0.16.1
flutter_socket_io: ^0.6.0 flutter_socket_io: ^0.6.0
sentry_flutter: ^4.0.4 catcher: ^0.4.1
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: