Skip to content

SessionInitializationException during GoogleMapsNavigator.initializeNavigationSession() #366

@Hamiz-Usmani

Description

@Hamiz-Usmani

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

I am encountering a SessionInitializationException when trying to initialize the navigation session using GoogleMapsNavigator.initializeNavigationSession(). This occurs after successfully requesting location permissions and attempting to show the terms and conditions dialog.

Flutter version

Flutter 3.27.4 • channel stable

Package version

^0.6.0

Native SDK versions

  • I haven't changed the version of the native SDKs

Flutter Doctor Output

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.27.4, on macOS 15.4.1 24E263 darwin-x64, locale en-AE)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.99.3)
[✓] VS Code (version 1.94.2)
[✓] Connected device (3 available)
! Error: Browsing on the local area network for AKRO’s iPhone. Ensure the device is unlocked and
attached with a cable or associated with the same local area network as this Mac.
The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

• No issues found!

Steps to reproduce

Steps to Reproduce:
Set up a new Flutter project with google_navigation_flutter and permission_handler.
Configure Android and iOS projects as per the documentation (API keys, minSdkVersion, Podfile, Info.plist background modes, etc.).
Implement the basic navigation view setup similar to the example provided in the plugin's README or the code below.
Run the app on an Android device/emulator.
Grant location permission when prompted.
The app then attempts to initialize the navigation session.

Expected vs Actual Behavior

Expected Behavior:
The navigation session should initialize successfully, and the GoogleMapsNavigationView should be displayed if terms are accepted.
Actual Behavior:
The GoogleMapsNavigator.initializeNavigationSession() call throws a SessionInitializationException.
Flutter console output:

Code Sample

// Example code hereimport 'package:flutter/material.dart';
import 'package:google_navigation_flutter/google_navigation_flutter.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Navigation Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const NavigationScreen(),
    );
  }
}

class NavigationScreen extends StatefulWidget {
  const NavigationScreen({super.key});

  @override
  State<NavigationScreen> createState() => _NavigationScreenState();
}

class _NavigationScreenState extends State<NavigationScreen> {
  GoogleNavigationViewController? _navigationViewController;
  bool _navigationSessionInitialized = false;
  bool _isLoading = true;
  String _statusMessage = 'Requesting permissions...';

  @override
  void initState() {
    super.initState();
    _requestPermissionsAndInitialize();
  }

  Future<void> _requestPermissionsAndInitialize() async {
    setState(() {
      _statusMessage = 'Requesting location permission...';
    });
    final PermissionStatus status = await Permission.location.request();

    if (status.isGranted) {
      setState(() {
        _statusMessage =
            'Location permission granted. Initializing navigation...';
      });
      await _initializeNavigationSession();
    } else if (status.isDenied) {
      setState(() {
        _statusMessage =
            'Location permission denied. Please grant permission to use navigation.';
        _isLoading = false;
      });
    } else if (status.isPermanentlyDenied) {
      setState(() {
        _statusMessage =
            'Location permission permanently denied. Please open app settings to grant permission.';
        _isLoading = false;
      });
      // Optionally, open app settings:
      // await openAppSettings();
    }
  }

  Future<void> _initializeNavigationSession() async {
    // IMPORTANT: Replace with your actual company name and app title for T&C
    const String termsAndConditionsDialogTitle = 'Navigation Terms';
    const String termsAndConditionsDialogCompanyName = 'Your Company Name';

    try {
      if (!await GoogleMapsNavigator.areTermsAccepted()) {
        final bool termsAccepted =
            await GoogleMapsNavigator.showTermsAndConditionsDialog(
          termsAndConditionsDialogTitle,
          termsAndConditionsDialogCompanyName,
        );
        if (!termsAccepted) {
          setState(() {
            _statusMessage =
                'Terms and Conditions not accepted. Navigation cannot start.';
            _isLoading = false;
          });
          return;
        }
      }

      // IMPORTANT: Ensure you have configured your API keys in native Android/iOS projects.
      // This SDK relies on native Google Navigation SDKs which need API keys set up.
      // Refer to the Google Navigation SDK documentation for Flutter for API key setup.

      // Initialize the navigation session
      // Consider `taskRemovedBehavior` for Android if needed.
      await GoogleMapsNavigator.initializeNavigationSession();

      setState(() {
        _navigationSessionInitialized = true;
        _isLoading = false;
        _statusMessage = 'Navigation session initialized.';
      });
    } catch (e) {
      setState(() {
        _statusMessage = 'Error initializing navigation: ${e.toString()}';
        _isLoading = false;
      });
      // Log the error or show a more user-friendly message
      print('Error initializing navigation session: $e');
    }
  }

  void _onViewCreated(GoogleNavigationViewController controller) {
    _navigationViewController = controller;
    // Enable My Location layer. Requires location permission to be granted.
    _navigationViewController?.setMyLocationEnabled(true);

    // You can now use the controller to start navigation, set destinations, etc.
    // Example: Set a destination (replace with actual LatLng)
    // _navigationViewController?.setDestination(
    //   waypoint: NavigationWaypoint.withLatLng(
    //     title: 'Destination',
    //     latitude: 37.4219983,
    //     longitude: -122.084,
    //   ),
    // );
    // _navigationViewController?.startGuidance();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Google Navigation'),
      ),
      body: _isLoading
          ? Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const CircularProgressIndicator(),
                  const SizedBox(height: 16),
                  Text(_statusMessage),
                ],
              ),
            )
          : _navigationSessionInitialized
              ? GoogleMapsNavigationView(
                  onViewCreated: _onViewCreated,
                  initialNavigationUIEnabledPreference:
                      NavigationUIEnabledPreference.disabled, // Or .enabled
                  // You can further customize the view here, e.g.:
                  // cameraTargetBounds: CameraTargetBounds.unbounded(),
                  // mapType: MapType.normal,
                  // compassEnabled: true,
                  // rotateGesturesEnabled: true,
                  // scrollGesturesEnabled: true,
                  // tiltGesturesEnabled: true,
                  // zoomControlsEnabled: true,
                  // zoomGesturesEnabled: true,
                )
              : Center(
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Text(
                      _statusMessage,
                      textAlign: TextAlign.center,
                    ),
                  ),
                ),
    );
  }

  @override
  void dispose() {
    if (_navigationSessionInitialized) {
      // It's crucial to cleanup the navigation session
      GoogleMapsNavigator.cleanup();
    }
    super.dispose();
  }
}

Additional Context

No response

Metadata

Metadata

Assignees

Labels

priority: p0Highest priority. Critical issue. P0 implies highest priority.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions