Skip to Content
SDKSDKsXenseAR Native SDKFlutterXenseAR SDK Usage Guide

XenseAR SDK Usage Guide for Flutter

Prerequisites

  1. The following files from the Template Project:
    • main.dart
    • web_view_page.dart
    • xense_ar.dart
  2. The Flutter project you want to integrate with.

Usage Steps

1. Copy xense_ar.dart into your Flutter project:

import 'dart:async'; import 'package:flutter/services.dart'; class XenseAR { static const MethodChannel _channel = MethodChannel('xense_ar'); static const EventChannel _eventChannel = EventChannel('xense_ar/events'); static Stream<String> get unityMessages { return _eventChannel .receiveBroadcastStream() .map((event) => event.toString()); } static Future<bool> openUnity() async { final result = await _channel.invokeMethod<bool>( 'openUnity', ); return result ?? false; } static Future<String> sendCommand( String command, String value, ) async { final result = await _channel.invokeMethod<String>( 'sendCommand', { 'command': command, 'value': value, }, ); return result ?? ''; } }

2. Refer to web_view_page.dart and prepare the required components to use the SDK

Import the required packages:

import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'xense_ar.dart';

Create a StatefulWidget and add the following logic to handle Unity messages and launch the AR experience.

Note: Replace the placeholder values provided_by_xense with the values provided by the Xense team. Please contact your SDK provider for the required configuration.

StreamSubscription<String>? _unitySubscription; @override void initState() { super.initState(); debugPrint('WEBVIEW PAGE INIT'); _listenUnityMessages(); } Future<void> _openUnity() async { try { await XenseAR.openUnity(); } catch (e) { debugPrint('Failed to open Unity: $e'); } } void _listenUnityMessages() { _unitySubscription = XenseAR.unityMessages.listen( (message) { debugPrint( 'MESSAGE FROM UNITY: $message', ); _handleUnityMessage(message); }, onError: (error) { debugPrint( 'UNITY EVENT ERROR: $error', ); }, ); } Future<void> _handleUnityMessage(String message) async { if (message.contains('webviewReady')) { try { final symmetricKeyResponse = await XenseAR.sendCommand( 'updateSymmetricKey', 'provided_by_xense', ); debugPrint( 'updateSymmetricKey SUCCESS: $symmetricKeyResponse', ); final encryptedConfigResponse = await XenseAR.sendCommand( 'updateEncryptedConfig', 'provided_by_xense', ); debugPrint( 'updateEncryptedConfig SUCCESS: $encryptedConfigResponse', ); } on PlatformException catch (e) { debugPrint( 'update config ERROR: ${e.code} - ${e.message}', ); } } else if ( message.contains('unityReady') || message.contains('backToHomeScreen')) { try { final response = await XenseAR.sendCommand( 'updateHomeScreenUrl', 'provided_by_xense', ); debugPrint( 'updateHomeScreenUrl SUCCESS: $response', ); } on PlatformException catch (e) { debugPrint( 'updateHomeScreenUrl ERROR: ${e.code} - ${e.message}', ); } } else if (message.contains('getNativeParameters')) { try { final response = await XenseAR.sendCommand( 'updateNativeParameters', r'{\"userID\": \"abc\", \"auth_key\": \"xyz\"}', ); debugPrint( 'updateNativeParameters SUCCESS: $response', ); } on PlatformException catch (e) { debugPrint( 'updateNativeParameters ERROR: ${e.code} - ${e.message}', ); } } } @override void dispose() { _unitySubscription?.cancel(); super.dispose(); }

3. You’re ready to run the application

flutter clean flutter run -d <your-device>
Last updated on