Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ repomix-output.txt
*.log
.env
*.tmp

.DS_Store
Binary file removed android/.DS_Store
Binary file not shown.
Binary file removed android/src/.DS_Store
Binary file not shown.
Binary file removed android/src/main/.DS_Store
Binary file not shown.
Binary file removed android/src/main/assets/.DS_Store
Binary file not shown.
Binary file removed android/src/main/kotlin/.DS_Store
Binary file not shown.
Binary file removed android/src/main/res/.DS_Store
Binary file not shown.
Binary file removed examples/.DS_Store
Binary file not shown.
Binary file removed lib/.DS_Store
Binary file not shown.
22 changes: 15 additions & 7 deletions lib/managers/ar_session_manager.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math' show sqrt;
import 'dart:typed_data';
import 'dart:io';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since Platform.isAndroid can be replaced with defaultTargetPlatform, this import is no longer needed and should be removed to keep the imports clean and avoid unnecessary dependencies on dart:io.


import 'package:ar_flutter_plugin_2/datatypes/config_planedetection.dart';
import 'package:ar_flutter_plugin_2/models/ar_anchor.dart';
Expand Down Expand Up @@ -49,9 +50,18 @@ class ARSessionManager {
/// Returns the camera pose in Matrix4 format with respect to the world coordinate system of the [ARView]
Future<Matrix4?> getCameraPose() async {
try {
final serializedCameraPose =
await _channel.invokeMethod<List<dynamic>>('getCameraPose', {});
return MatrixConverter().fromJson(serializedCameraPose!);
if (Platform.isAndroid) {
final serializedCameraPose = await _channel.invokeMethod<Map<dynamic, dynamic>>('getCameraPose', {});
final position = serializedCameraPose!['position'];
final rotation = serializedCameraPose!['rotation'];

final translation = Vector3(position!['x'], position!['y'], position!['z']);
final quaternion = Quaternion(rotation!['x'], rotation!['y'], rotation!['z'], rotation!['w']);
return Matrix4.compose(translation, quaternion, Vector3.all(1.0));
} else {
final serializedCameraPose = await _channel.invokeMethod<List<dynamic>>('getCameraPose', {});
return MatrixConverter().fromJson(serializedCameraPose!);
}
Comment on lines +53 to +64

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using the null assertion operator (!) repeatedly on dynamic map lookups (e.g., serializedCameraPose!['position'] and position!['x']) is risky and can easily lead to runtime crashes if any key is missing or null.

Additionally, values returned from platform channels for numeric coordinates can sometimes be serialized as int (e.g., 0 instead of 0.0). Passing an int directly to Vector3 or Quaternion constructors (which expect double) will throw a runtime TypeError in Dart.

Furthermore, using Platform.isAndroid from dart:io makes the code harder to unit test and can cause issues in web environments. Since package:flutter/material.dart is already imported, we can use defaultTargetPlatform == TargetPlatform.android instead.

Here is a safer, more robust implementation using defensive checks, safe double conversions, and defaultTargetPlatform:

      if (defaultTargetPlatform == TargetPlatform.android) {
        final serializedCameraPose = await _channel.invokeMethod<Map<dynamic, dynamic>>('getCameraPose', {});
        if (serializedCameraPose == null) return null;

        final position = serializedCameraPose['position'] as Map?;
        final rotation = serializedCameraPose['rotation'] as Map?;
        if (position == null || rotation == null) return null;

        final translation = Vector3(
          (position['x'] as num).toDouble(),
          (position['y'] as num).toDouble(),
          (position['z'] as num).toDouble(),
        );
        final quaternion = Quaternion(
          (rotation['x'] as num).toDouble(),
          (rotation['y'] as num).toDouble(),
          (rotation['z'] as num).toDouble(),
          (rotation['w'] as num).toDouble(),
        );
        return Matrix4.compose(translation, quaternion, Vector3.all(1.0));
      } else {
        final serializedCameraPose = await _channel.invokeMethod<List<dynamic>>('getCameraPose', {});
        if (serializedCameraPose == null) return null;
        return MatrixConverter().fromJson(serializedCameraPose);
      }

} catch (e) {
print('Error caught: ' + e.toString());
return null;
Expand Down Expand Up @@ -122,7 +132,7 @@ class ARSessionManager {
}

//Show or hide planes
void showPlanes(bool showPlanes){
void showPlanes(bool showPlanes) {
_channel.invokeMethod<void>('showPlanes', {
"showPlanes": showPlanes,
});
Expand All @@ -138,8 +148,7 @@ class ARSessionManager {
if (onError != null) {
onError!(call.arguments[0]);
print(call.arguments);
}
else{
} else {
ScaffoldMessenger.of(buildContext).showSnackBar(SnackBar(
content: Text(call.arguments[0]),
action: SnackBarAction(
Expand Down Expand Up @@ -207,7 +216,6 @@ class ARSessionManager {
});
}


/// Dispose the AR view on the platforms to pause the scenes and disconnect the platform handlers.
/// You should call this before removing the AR view to prevent out of memory erros
dispose() async {
Expand Down