下面是我的代码,现在工作很好,我已经初始化了共享偏好变量,并用它来检查它是否为空,如果不是空,它将导航到主页,如果为空,那么它将留在登录页面。我已经在Material App中初始化了共享偏好,但现在的问题是,我想使用有状态的My App类在我的应用中使用One Signal实现推送通知。我在这里附加了一个信号代码,我想在我的应用程序中实现,但因为我已经在runapp中使用了材料应用程序,我面临着问题。请给我一个解决办法。
当前主要。使用共享首选项正常工作的Dart文件:
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
var usrEmail = prefs.getString('email');
//print(email);
// await SystemChrome.setPreferredOrientations([
// DeviceOrientation.portraitUp,
// DeviceOrientation.portraitDown,
// ]);
runApp(
//MultiProvider(
// providers: [ChangeNotifierProvider(create: (_) => ChangeStatus())],
MaterialApp(
debugShowCheckedModeBanner: false,
title: "ATDOCHUB Dev",
// theme: ThemeData(
// colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.blue)
// .copyWith(secondary: Colors.white70),
// ),
home: usrEmail == null ? LoginPage() : homePageAdmin(),
));
}
主要。我想为推送通知实现的dart文件:
import 'package:flutter/material.dart';
import 'package:newsapp/screens/home_screen.dart';
import 'package:newsapp/screens/notification.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
import './utilities/globals.dart' as globals;
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// This widget is the root of your application.
@override
void initState() {
// TODO: implement initState
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);
OneSignal.shared
.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
debugPrint('NOTIFICATION OPENED HANDLER CALLED WITH: $result');
setState(() {});
globals.appnavigator.currentState
?.push(MaterialPageRoute(builder: (context) => const NotificationHome()));
});
OneSignal.shared.setNotificationWillShowInForegroundHandler(
(OSNotificationReceivedEvent event) {
setState(() {});
});
await OneSignal.shared.setAppId("d27b4c45-540c-41fb-b0c2-0f92cd13a664");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.indigo,
),
home: const HomeScreen(),
);
}
}
首先为共享首选项创建一个单独的服务。
class SharedPrefService {
static late SharedPreferences pref;
static Future<void> init() async {
pref = await SharedPreferences.getInstance();
}
}
现在在main函数中调用这个init方法,如下所示。
main() async {
WidgetsFlutterBinding.ensureInitialized();
await SharedPrefService.init();
}
现在你可以在整个应用程序中使用这个实例,只要调用
SharedPrefService.pref.///
不需要在任何地方初始化共享首选项,只需使用单个实例。如果你想在隔离或后台服务中使用sharedpreferences,那么你将需要再次初始化共享首选项,因为不同的线程。