rekabhq / background_locator

A Flutter plugin for updating location in background.
MIT License
288 stars 328 forks source link

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Null check operator used on a null value #221

Closed s15-coder closed 3 years ago

s15-coder commented 3 years ago

I am doing a basic app to see how the packages works following documentation. However, i'm getting this error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Null check operator used on a null value
E/flutter (15615): #0      SettingsUtil._getCommonArgumentsMap
package:background_locator/utils/settings_util.dart:46
E/flutter (15615): #1      SettingsUtil.getArgumentsMap
package:background_locator/utils/settings_util.dart:18
E/flutter (15615): #2      BackgroundLocator.registerLocationUpdate
package:background_locator/background_locator.dart:37
E/flutter (15615): #3      startLocationService
package:locator_implement/main.dart:46
E/flutter (15615): #4      _MyAppState.build.<anonymous closure>
package:locator_implement/main.dart:23
E/flutter (15615): #5      _FutureBuilderState.build
package:flutter/…/widgets/async.dart:773
E/flutter (15615): #6      StatefulElement.build

This is all my code (I'm using just one for test):

import 'package:background_locator/location_dto.dart';
import 'package:background_locator/settings/android_settings.dart';
import 'package:background_locator/settings/locator_settings.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: FutureBuilder(
          future: init(),
          builder: (context, AsyncSnapshot<bool> snapshot) {
            if (snapshot.data == true) {
              startLocationService();
            }
            return Scaffold(
              appBar: AppBar(
                title: Text('Material App Bar'),
              ),
              body: Center(
                child: Container(
                  child: Text('Hello World'),
                ),
              ),
            );
          }),
    );
  }
}

Future<bool> init() async {
  await BackgroundLocator.initialize();
  return true;
}

void startLocationService() {
  BackgroundLocator.registerLocationUpdate(callBack,
      initCallback: (as) {
        // print('===============INIT=================');
      },
      initDataCallback: {},
      disposeCallback: () {
        // print('==================DISPOSE=======================');
      },
      autoStop: false,
      androidSettings: AndroidSettings(
          accuracy: LocationAccuracy.HIGH,
          interval: 60,
          distanceFilter: 0,
          androidNotificationSettings: AndroidNotificationSettings(
              notificationChannelName: 'Location tracking',
              notificationTitle: 'Te estamos rastreandos',
              notificationMsg: 'aaaaaaaaaaaaaaaaaa',
              notificationBigMsg: 'ddddddddddddddddddddddddddddddddddddd',
              notificationIcon: '',
              notificationIconColor: Colors.grey,
              notificationTapCallback: () {
                print('===========TAP IN NOTIFICATION==================');
              })));
}

callBack(LocationDto location) {
  print('*/////////////////////${DateTime.now()}////////////////////');
}

The manifest.xml file:

    package="com.example.locator_implement">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <application
        android:label="locator_implement"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
            <service android:name="rekab.app.background_locator.IsolateHolderService"
            android:permission="android.permission.FOREGROUND_SERVICE"
            android:exported="true"
            android:foregroundServiceType = "location"/>

        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

Know anyone how can i fix it? I really need it plugin

s15-coder commented 3 years ago

It's was strange. I solve it separating and passing just the reference of every function, I mean:

BAD WAY

initCallback: (params){
     //code to execute
}

GOOD WAY


-------
initCallback: myInitCallback,
------

void myInitCallback(){
     //code to execute
}
badaszszsz commented 3 years ago

Hi @s15-coder could You test if adding

WidgetsFlutterBinding.ensureInitialized();

https://stackoverflow.com/a/68534628/16323705

just here:

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    WidgetsFlutterBinding.ensureInitialized();
    return MaterialApp(
      title: 'Material App',
      home: FutureBuilder(
          future: init(),

or changing

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// init your dependency injection here
runApp(MyApp());}

would do the click?

MoacirSchmidt commented 2 years ago

callback must be static