ui_background_task 0.3.0 copy "ui_background_task: ^0.3.0" to clipboard
ui_background_task: ^0.3.0 copied to clipboard

PlatformiOS

A Flutter plugin for extending short-running iOS work after an app enters the background, with CocoaPods and Swift Package Manager support.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:ui_background_task/ui_background_task.dart';

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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  int? _taskId;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Background Task Example app'),
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ElevatedButton(
                onPressed: () async {
                  final taskId =
                      await UiBackgroundTask.instance.beginBackgroundTask();
                  if (!mounted) return;
                  setState(() {
                    _taskId = taskId == 0 ? null : taskId;
                  });
                },
                child: const Text('Begin background task'),
              ),
              const SizedBox(height: 24),
              if (_taskId != null)
                ElevatedButton(
                  onPressed: () async {
                    await UiBackgroundTask.instance.endBackgroundTask(_taskId!);
                    if (!mounted) return;
                    setState(() {
                      _taskId = null;
                    });
                  },
                  child: Text(
                    'End background task: $_taskId',
                  ),
                )
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    UiBackgroundTask.instance.appLifeCycleUpdate(state);
  }
}
3
likes
160
points
1.25k
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for extending short-running iOS work after an app enters the background, with CocoaPods and Swift Package Manager support.

Repository (GitHub)
View/report issues

Topics

#background-task #flutter-plugin #ios

License

MIT (license)

Dependencies

flutter, plugin_platform_interface, stop_watch_timer

More

Packages that depend on ui_background_task

Packages that implement ui_background_task