我有一个项目需要我在后台访问共享首选项以确定需要设置哪些通知。使用它时,在模拟后台获取时出现此错误:
2021-11-12 10:15:33.351668-0600 Runner[88454:1012638] [VERBOSE-2:ui_dart_state.cc(209)] 未处理的异常:MissingPluginException(在通道 plugins.flutter.io/shared_preferences 上找不到方法 getAll 的实现)
main.dart
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:workmanager/workmanager.dart' as w;
w.Workmanager workmanager = w.Workmanager();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await workmanager.initialize(callbackDispatcher, isInDebugMode: true);
runApp(const MyApp());
//setSwitches();
}
void setSwitches() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences myPrefs = await SharedPreferences.getInstance();
myPrefs.setString('key', 'value');
print("setSwitch func");
}
void callbackDispatcher() {
print("callBackDispatcher");
workmanager.executeTask((task, inputData) async {
setSwitches();
return Future.value(true);
});
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
AppDelegate.swift
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
UNUserNotificationCenter.current().delegate = self
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert) // shows banner even if app is in foreground
}
class AppDelegate:UIResponder,UIApplicationDelegate{
func application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey:Any]?)->Bool{
// Other intialization code…
UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
return true
}
}
}
这是一个仅安装了 workmanager 和共享首选项的新项目。我需要在后台调用共享首选项,但到目前为止我找不到任何解决此问题的方法。我真的不想在项目的这一点上更改包,所以如果有一些解决方法你可能会看到我不是,我会很感激。