Below is my code which is working fine for now where I have initialised shared preferences variable and used it to check if it is null or not if not null it will navigate to homepage if null then it will stay on login Page.I have initialised shared preferences in Material App but now problem is that i wants to use statefull My App class to implement push notification in my app using One Signal. I am attaching one signal code also here which i wants to implement in my app, but as I have already using material app in runapp i am facing problem. Please suggest me a solution for this.
Current main.dart file which is working properly using shared preferences:
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(),
));
}
main.dart file which I wants to implement for push notification:
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(),
);
}
}
First of all make a separate service for shared preferences.
class SharedPrefService {
static late SharedPreferences pref;
static Future<void> init() async {
pref = await SharedPreferences.getInstance();
}
}
Now call this init method in the main function as following.
main() async {
WidgetsFlutterBinding.ensureInitialized();
await SharedPrefService.init();
}
Now you can use this single instance all over the app where you want, just call the
SharedPrefService.pref.///
No need to initialize shared preferences everywhere just use a single instance. If you want to use sharedpreferences in isolate or in background service then you will need to initialize shared preferences again because of different threads.