Reputation: 5042
Here is a simple code to detect vertical position of device
But I don't found how to run this code in background with flutter
I try to use isolate but console print only 20s after foreground
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:sensors/sensors.dart';
import 'package:flutter_isolate/flutter_isolate.dart';
void main() async {
runApp(MyApp());
}
logic() async {
accelerometerEvents.listen((AccelerometerEvent event) {
print(event.y);
if (event.y>5.0||event.y<-5.0){
print("notification fire"); }
});
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Future<void> _run() async {
final isolate = await FlutterIsolate.spawn(logic(), "test");
Timer(Duration(seconds: 1), () {
print("Resuming Isolate 1");
isolate.resume();
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: ElevatedButton(
child: Text('Run'),
onPressed: _run,
),
),
),
);
}
}
Upvotes: 0
Views: 132
Reputation: 525
Please review the documentation for info here: https://api.dart.dev/stable/2.12.2/dart-isolate/dart-isolate-library.html
There are two plugins you should take a look at:
https://pub.dev/packages/background_fetch
https://pub.dev/packages/flutter_isolate
Upvotes: 1