The code is here. I add getTemporaryDirectory in main.but It make the screen white.
Future<void> main() async {
await getTemporaryDirectory();
runApp(MyApp());
}
Do this instead:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized(); // new line
await getTemporaryDirectory();
runApp(MyApp());
}
Read more about the ensureInitialized method here : WidgetsBinding ensureInitialized
Related
How to register multiple TypeAdapter in Flutter Hive?
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
if (!Hive.isAdapterRegistered(ModelOneAdapter().typeId)) {
Hive.registerAdapter(ModelOneAdapter());
}
if (!Hive.isAdapterRegistered(ModelTwoAdapter().typeId)) {
Hive.registerAdapter(ModelTwoAdapter());
}
runApp(const MyApp());
}
This is my code and I can't register the second one.
ModelTwoAdapter()
After carefully trying other answer, nothing seems to work.
Notification are working fine, when I click on them and the app has been initialised.
When the app is closed or terminated it is impossible for me to redirect the user to a page because the value of the getInitialMessage() is null.
For example, this return null, but it is the home page of my Flutter app
#override
void initState() {
super.initState();
FirebaseMessaging.instance
.getInitialMessage()
.then((message) => print('Initial message: $message'))
Main.dart
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
runApp(const FrontDeskApp());
}
Thank you
This may be the reason because you are using getInitialMessage() method in wrong way.
Modify your main.dart like the below way and test.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
await FirebaseMessaging.instance.getInitialMessage().then((remoteMessage) {
// whenever you open the app from the terminate state by clicking on Notification message,
here you will get your remoteMessage.
// Set you App Screen Redirection here.
// Once consumed, the RemoteMessage will be removed.
});
runApp(const MyApp());
}
For more reference please refer to the official document for Handling Interaction of Notification
I am a Beginner in flutter and i was learning hydratedBloc, i imported all the needed dependencies as in the tutorial i was following and i run into a problem where my HydratedBlocOverrrides.runZoned is marked as an error
[This is the screenshot containing the errorthis is the Bloc_Imports files containg the exported hydratedBloc](https://i.stack.imgur.com/fVTcR.png)
Why do i have this error?
I tried using HydratedBloc to locally store data,i imported hydratedBloc but this syntax was marked and error "Undefined Name" refering to this HydratedBlocOverrrides.runZoned
According to hydrated_bloc's Changelog in version 9.0.0. It has removed HydratedBlocOverrides
BREAKING: feat!: reintroduce HydratedBloc.storage and remove HydratedBlocOverrides (#3479)
upgrade to bloc: ^8.1.0
Therefore change your code from:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final storage = await HydratedStorage.build(
storageDirectory: await getApplicationDocumentsDirectory());
HydratedBlocOverrides.runZoned(
() => runApp(const MyApp()),
storage: storage,
);
}
to
void main() async {
WidgetsFlutterBinding.ensureInitialized();
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: await getTemporaryDirectory(),
);
runApp(const MyApp());
}
I've just changed my projects to release mode and its suddenly make my SharedPreferences doesn't work as it works before the change. The value is always return as a 'null' even I save it as 1.
This is how I save the data
_storeOnBoardInfo() async {
int isViewed = 1;
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt('onBoard', isViewed);
}
and this is how a load the data
void main() async {
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
statusBarColor: Colors.white, statusBarIconBrightness: Brightness.dark));
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
isViewed = prefs.getInt('onBoard');
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
runApp(const MyApp());
FlutterNativeSplash.remove();
}
I tried some solutions but it doesn't work for me. Is there any other way to make solve this problem?
I have a problem while working with pat_provider.
Error:
Unhandled Exception: Null check operator used on a null value
Code:
void main() async {
final appDocumentDirectory =
await path_provider.getApplicationDocumentsDirectory();
Hive.init(appDocumentDirectory.path);
runApp(MyApp());
}
For more, I am using the non-nullsafety app, Right now I want to take the path to hive. what would you advise in such a case. Or should I downgrade the path_provider version?
Just add two lines: import material.dart and add WidgetsFlutterBinding.ensureInitialized();
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final appDocumentDirectory =
await path_provider.getApplicationDocumentsDirectory();
Hive.init(appDocumentDirectory.path);
runApp(MyApp());
}
Resolved! I was doing hive the old way. The program worked when I called Hive.initFlutter(), not Hive.init(). By the way,
import 'package: hive_flutter / hive_flutter.dart';
and
import 'package: hive / hive.dart';
Don't forget to import both,
Current code:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
runApp(CoozinCustomerApp());
}