run app with runZonedGuarded

This commit is contained in:
Vadim 2024-01-27 21:33:10 +01:00
parent 147fedc36f
commit dc9aa7bc1d
6 changed files with 51 additions and 64 deletions

4
.vscode/launch.json vendored
View file

@ -41,7 +41,7 @@
"--dart-define",
"cameraPreviewAspectRatio=240/320",
],
"program": "${workspaceFolder}/lib/main_release.dart",
"program": "${workspaceFolder}/lib/main_prod.dart",
},
{
"name": "prod-profile (android)",
@ -54,7 +54,7 @@
"--dart-define",
"cameraPreviewAspectRatio=240/320",
],
"program": "${workspaceFolder}/lib/main_release.dart",
"program": "${workspaceFolder}/lib/main_prod.dart",
},
{
"name": "dev-debug (ios)",

View file

@ -9,11 +9,19 @@ class LightmeterAnalytics {
const LightmeterAnalytics({required ILightmeterAnalyticsApi api}) : _api = api;
void init() {
FlutterError.onError = (details) => logCrash(details.exception, details.stack);
PlatformDispatcher.instance.onError = (error, stack) {
logCrash(error, stack);
return true;
};
}
Future<void> logEvent(
String eventName, {
Map<String, dynamic>? parameters,
}) async {
if (kDebugMode) {
if (!kReleaseMode) {
log('<LightmeterAnalytics> logEvent: $eventName / $parameters');
return;
}
@ -26,17 +34,17 @@ class LightmeterAnalytics {
Future<void> logCrash(
dynamic exception,
StackTrace? stack, {
StackTrace? stackTrace, {
dynamic reason,
Iterable<Object> information = const [],
}) async {
if (kDebugMode) {
if (!kReleaseMode) {
return;
}
return _api.logCrash(
exception,
stack,
stackTrace,
reason: reason,
information: information,
);

View file

@ -1,22 +0,0 @@
import 'dart:developer';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:lightmeter/firebase_options.dart';
Future<void> initializeFirebase({required bool handleErrors}) async {
try {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
if (handleErrors) {
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
PlatformDispatcher.instance.onError = (error, stack) {
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
return true;
};
}
} catch (e) {
log(e.toString());
}
}

View file

@ -1,19 +1,4 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/application.dart';
import 'package:lightmeter/application_wrapper.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/firebase.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
import 'package:lightmeter/runner.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeFirebase(handleErrors: true);
runApp(
const IAPProductsProvider(
child: ApplicationWrapper(
Environment.prod(),
child: Application(),
),
),
);
}
Future<void> main() => runLightmeterApp(const Environment.prod());

View file

@ -1,19 +0,0 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/application.dart';
import 'package:lightmeter/application_wrapper.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/firebase.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeFirebase(handleErrors: false);
runApp(
const IAPProductsProvider(
child: ApplicationWrapper(
Environment.prod(),
child: Application(),
),
),
);
}

35
lib/runner.dart Normal file
View file

@ -0,0 +1,35 @@
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/widgets.dart';
import 'package:lightmeter/application.dart';
import 'package:lightmeter/application_wrapper.dart';
import 'package:lightmeter/data/analytics/analytics.dart';
import 'package:lightmeter/data/analytics/api/analytics_firebase.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/firebase_options.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
const _errorsLogger = LightmeterAnalytics(api: LightmeterAnalyticsFirebase());
Future<void> runLightmeterApp(Environment env) async {
runZonedGuarded(
() async {
WidgetsFlutterBinding.ensureInitialized();
if (env.buildType == BuildType.prod) {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
}
_errorsLogger.init();
final application = ApplicationWrapper(env, child: const Application());
runApp(
env.buildType == BuildType.dev
? IAPProducts(
products: [IAPProduct(storeId: IAPProductType.paidFeatures.storeId)],
child: application,
)
: IAPProductsProvider(child: application),
);
},
_errorsLogger.logCrash,
);
}