Skip to content

[Bug]: ERROR: SessionInitializationError.termsNotAccepted --> when calling await gnav.GoogleMapsNavigator.initializeNavigationSession(); #399

@jasonlaw-lscm

Description

@jasonlaw-lscm

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

When using google_navigation_flutter library, error occur when calling GoogleMapsNavigator.initializeNavigationSession()

SessionInitializationException: SessionInitializationError.termsNotAccepted

Inside library: lib\src\navigator\google_navigation_flutter_navigator.dart

/// Possible errors that [GoogleMapsNavigator.initializeNavigationSession] can throw.
/// {@category Navigation}
enum SessionInitializationError {
  /// The session initialization failed, because the required Maps API key is empty or invalid.
  notAuthorized,

  /// The session initialization failed, because the required location permission has not been granted.
  locationPermissionMissing,

  /// The session initialization failed, because the user has not yet accepted the navigation terms and conditions.
  termsNotAccepted
}

On android device, there is no pop-up terms & condition abt navigation. Only location permission pop-up appears.

Flutter version

3.32.1

Package version

0.6.1

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.32.1, on Microsoft Windows [Version 10.0.22631.5039], locale en)
[√] Windows Version (11 64-bit, 23H2, 2009)
[√] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Build Tools 2019 16.11.36)
[√] Android Studio (version 2024.3)
[√] Connected device (5 available)
[√] Network resources

• No issues found!

flutter doctor -v
[√] Flutter (Channel stable, 3.32.1, on Microsoft Windows [Version 10.0.22631.5039], locale en) [542ms]
• Flutter version 3.32.1 on channel stable at C:\flutter\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision b25305a883 (5 weeks ago), 2025-05-29 10:40:06 -0700
• Engine revision 1425e5e9ec
• Dart version 3.8.1
• DevTools version 2.45.1

[√] Windows Version (11 64-bit, 23H2, 2009) [6.6s]

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.1) [1,598ms]
• Android SDK at C:\Users\lscm-\AppData\Local\Android\sdk
• Platform android-35, build-tools 35.0.1
• Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
This is the JDK bundled with the latest Android Studio installation on this machine.
To manually set the JDK path, use: flutter config --jdk-dir="path/to/jdk".
• Java version OpenJDK Runtime Environment (build 21.0.6+-13355223-b631.42)
• All Android licenses accepted.

[√] Chrome - develop for the web [118ms]
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Build Tools 2019 16.11.36) [117ms]
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools
• Visual Studio Build Tools 2019 version 16.11.34902.97
• Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2024.3) [15ms]
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 21.0.6+-13355223-b631.42)

[√] Connected device (5 available) [298ms]
• SM A3460 (mobile) • RFCX305MJ7T • android-arm64 • Android 14 (API 34)
• sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64 • Android 16 (API 36) (emulator)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22631.5039]
• Chrome (web) • chrome • web-javascript • Google Chrome 137.0.7151.104
• Edge (web) • edge • web-javascript • Microsoft Edge 137.0.3296.93

[√] Network resources [316ms]
• All expected network resources are available.

• No issues found!

Steps to reproduce

  • 2 files, main.dart & nav_test.dart
  • copy to run
  • click the navigation button on screen --> to enter navigation mode --> error

Expected vs Actual Behavior

EXPECTED to start the navigation mode.
Actual Behavior: failed at GoogleMapsNavigator.initializeNavigationSession()

Navigation error:
SessionInitializationException: SessionInitializationError.termsNotAccepted
0      NavigationSessionAPIImpl.createNavigationSession (package:google_navigation_flutter/src/method_channel/session_api.dart:68:11)
asynchronous suspension
1      GoogleMapsNavigator.initializeNavigationSession (package:google_navigation_flutter/src/navigator/google_navigation_flutter_navigator.dart:56:5)
asynchronous suspension
2      _NavTestState._startNavigation (package:flutter_application_1/nav_test.dart:36:7)
asynchronous suspension

Code Sample

inside nav_test.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_navigation_flutter/google_navigation_flutter.dart' as gnav;
import 'package:geolocator/geolocator.dart';

class NavTest extends StatefulWidget {
  const NavTest({Key? key}) : super(key: key);
  @override
  State<NavTest> createState() => _NavTestState();
}

class _NavTestState extends State<NavTest> {
  final Completer<gnav.GoogleNavigationViewController> _navController = Completer();
  gnav.LatLng? currentLocation;
  final gnav.LatLng destination = gnav.LatLng(latitude: 22.302711, longitude: 114.177216); // Example: Hong Kong
  String? _errorMessage; // <-- Add this field

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

  Future<void> _determinePosition() async {
    final pos = await Geolocator.getCurrentPosition();
    setState(() {
      currentLocation = gnav.LatLng(latitude: pos.latitude, longitude: pos.longitude);
    });
  }

  Future<void> _startNavigation() async {
    try {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Step 1: Initializing navigation session...')),
      );
      await gnav.GoogleMapsNavigator.initializeNavigationSession();
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Step 2: Setting destinations...')),
      );
      await gnav.GoogleMapsNavigator.setDestinations(
        gnav.Destinations(
          waypoints: [
            gnav.NavigationWaypoint.withLatLngTarget(
              title: 'Start',
              target: currentLocation!,
            ),
            gnav.NavigationWaypoint.withLatLngTarget(
              title: 'Destination',
              target: destination,
            ),
          ],
          displayOptions: gnav.NavigationDisplayOptions(showDestinationMarkers: true),
        ),
      );
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Step 3: Starting guidance...')),
      );
      await gnav.GoogleMapsNavigator.startGuidance();
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Step 4: Enabling navigation UI...')),
      );
      final controller = await _navController.future;
      await controller.setNavigationUIEnabled(true);
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Navigation UI should now be visible!')),
      );
      setState(() {
        _errorMessage = null; // Clear any previous error
      });
    } on gnav.SessionInitializationException catch (e, stack) {
      setState(() {
        _errorMessage =
            'Navigation error:\n'
            'SessionInitializationException: ${e.code}\n\n'
            '$stack\n${e}';
      });
    } catch (e, stack) {
      setState(() {
        _errorMessage = 'Navigation error:\n$e\n\n$stack';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    if (currentLocation == null) {
      return const Scaffold(body: Center(child: CircularProgressIndicator()));
    }
    return Scaffold(
      appBar: AppBar(title: const Text('Minimal Navigation Test')),
      body: Stack(
        children: [
          gnav.GoogleMapsNavigationView(
            onViewCreated: (controller) => _navController.complete(controller),
            initialCameraPosition: gnav.CameraPosition(target: currentLocation!, zoom: 16),
            initialMapType: gnav.MapType.normal,
          ),
          if (_errorMessage != null)
            Positioned(
              top: 60,
              left: 20,
              right: 20,
              child: Material(
                elevation: 8,
                color: Colors.red[100],
                borderRadius: BorderRadius.circular(8),
                child: Padding(
                  padding: const EdgeInsets.all(16),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          const Icon(Icons.error, color: Colors.red),
                          const SizedBox(width: 8),
                          const Text(
                            'Error',
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              color: Colors.red,
                              fontSize: 18,
                            ),
                          ),
                          const Spacer(),
                          IconButton(
                            icon: const Icon(Icons.close),
                            onPressed: () => setState(() => _errorMessage = null),
                          ),
                        ],
                      ),
                      const SizedBox(height: 8),
                      SingleChildScrollView(
                        scrollDirection: Axis.horizontal,
                        child: Text(
                          _errorMessage!,
                          style: const TextStyle(color: Colors.black, fontSize: 13),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _startNavigation,
        child: const Icon(Icons.navigation),
      ),
    );
  }
}

inside main:

// main.dart
import 'map_interface.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'nav_test.dart';

void main() => runApp(const MaterialApp(home: NavTest())); //for testing navigation mode

Additional Context

No response

Metadata

Metadata

Labels

priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.triage meI really want to be triaged.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