Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to enable background processes in Flutter?

I have this code in flutter that should send a notification every 3 seconds in the background (even when the app is completely closed):

    import 'dart:async';
    import 'dart:ui';
    import 'package:flutter_background_service_android/flutter_background_service_android.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_background_service/flutter_background_service.dart';
    import 'package:p2p_app/services/notifivcation_setrvice.dart';

    @pragma('vm:entry-point')
    void onStart(ServiceInstance service) {
      DartPluginRegistrant.ensureInitialized();

      int i = 0;

      Timer.periodic(const Duration(seconds: 3), (timer) async {
        i++;

        if (service is AndroidServiceInstance) {
          if (await service.isForegroundService()) {
            LocalNoticeService().addNotification(
                title: 'Test', body: i.toString(), id: i, channel: 'channel-alice');
            print('Hello $i');
          }
        }
      });
    }

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();

      await LocalNoticeService().setup();
      await initializeService();

      runApp(const MyApp());
    }

    Future<void> initializeService() async {
      final service = FlutterBackgroundService();

      await service.configure(
        androidConfiguration: AndroidConfiguration(
          onStart: (service) => onStart(service),
          autoStart: true,
          isForegroundMode: true,
        ),
        iosConfiguration: IosConfiguration(),
      );
    }

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

      @override
      _MyAppState createState() => _MyAppState();
    }

    class _MyAppState extends State<MyApp> {
      @override
      void initState() {
        super.initState();
      }

      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: Column(children: [
              ElevatedButton(onPressed: () async {}, child: const Text('Create')),
              ElevatedButton(onPressed: () async {}, child: const Text('Stop')),
            ]),
          ),
        );
      }
    }

But my application can’t even start because of this persistent error:

    E/flutter (12081): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: onStart method must be a top-level or static function
    E/flutter (12081): #0      FlutterBackgroundServiceAndroid.configure (package:flutter_background_service_android/flutter_background_service_android.dart:90:7)
    E/flutter (12081): #1      FlutterBackgroundService.configure (package:flutter_background_service/flutter_background_service.dart:20:17)
    E/flutter (12081): #2      initializeService (package:p2p_app/main.dart:140:17)
    E/flutter (12081): #3      main (package:p2p_app/main.dart:100:9)
    E/flutter (12081): <asynchronous suspension>

Does anyone know how to fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Ignore this text: people write to me that my post consists mainly of code. People write to me that my post consists mainly of code. People write to me that my post consists mainly of code.

>Solution :

Replace

AndroidConfiguration(
  onStart: (service) => onStart(service),
  autoStart: true,
  isForegroundMode: true,
)

with

AndroidConfiguration(
  onStart: onStart,
  autoStart: true,
  isForegroundMode: true,
)

The former, the type of onStart: is a lambda. The latter, is a function.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading