Why firebase not working with flutter web? - flutter

when I don't initialize
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
it chrome shows the UI, but when I add it displays nothing . How can I solve this error
chrome output,
code warning

You also have to initialize Firebase in index.html; here is the documentation.

Related

Flutter Local notifications - notification click is not working while the app in background

When application killed then notification click not working account to onSelectNotification and also big picture image notification not working when app in background.
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (action) {})
Handle background messages by registering a onBackgroundMessage handler. When messages are received, an isolate is spawned (Android only, iOS/macOS does not require a separate isolate) allowing you to handle messages even when your application is not running. try this:
#pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
}
void main() {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
follow full documentation here: https://firebase.google.com/docs/cloud-messaging/flutter/receive
There is no such thing as "in the background" with mobile devices. Mobile devices run one foreground app. When you "put it in the background" it is closed and a screenshot is kept to make you think it's "in the background". It's not. It's closed.
So it does not work when it's closed. That's normal. Because the app isn't running, it cannot execute code.
The app has first to be started again. To find out, whether your app was started by tapping a notification, you can use this line in your start up code:
final NotificationAppLaunchDetails notificationAppLaunchDetails =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
Source: Documentation
This way you can find out if your app was started from your notification and then act accordingly (for example by navigating to a different route depending on those details).

Flutter FireStore - initialize instance after clearing persistence

I am calling await FirebaseFirestore.instance.clearPersistence(); in-order to clear the cached data after a user signed out.
Per the documentation, you only call it after terminating the firestore session.
But after trying to retrieve the data again after login, I am getting an error:
"The client has already been terminated"
EDIT: THIS HAPPENS ONLY ON FLUTTER WEB. On flutter mobile (Tested on Android) it is working fine.
So how can I initialize the instance after terminating it?
await FirebaseFirestore.instance.terminate();
await FirebaseFirestore.instance.clearPersistence();

Flutter - Firebase.initializeApp() method doesn't work

I'm writing a flutter web app with Firebase.
The app is hosting on Firebase now and, to use Firebase Authentication service, I want Firebase.initializeApp() to be executed.
But there is a problem as mentioned on title.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const WebLoginApp());
}
When the app gets started, it shows only a white screen.
and also shows error log on DEBUG CONSOLE (case 1)
OR has break points with error log. (case 2)
Without await Firebase.initializeApp();, the app works well.
I've found another app I wrote for Firebase test has the same problem but it works well 3 weeks ago.
I've just updated Flutter and Chrome with latest version.
Could you give a hint or a solution?
It will be really appreciated.
You propably forgot the web installation for firebase, check this:
https://firebase.flutter.dev/docs/manual-installation/web
and you need initializeApp to this:
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);

Why is onTokenRefresh not firing?

I'm new to firebase messaging and flutter. According to the flutter firebase_messaging package docs, onTokenRefresh is fired when a new FCM token is generated. And according to Google's firebase docs there are two scenarios that triggers token generation:
When a new token is generated on initial app startup
Whenever an existing token is changed
Here is a simplified version of the main function of my application. After each execution, I delete the app from the emulator and the displayed token does indeed change. Despite this, onTokenRefresh is never fired and it should if my understanding of the documentation is correct.
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.instance.onTokenRefresh.listen((String token) {
print("New token: $token");
});
String token = await FirebaseMessaging.instance.getToken();
print("Token: $token");
//runApp(MyApp());
}
As I said, I'm new to flutter, dart and firebase messaging, is there something I'm fundamentally misunderstanding? Thanks.
So I think I figured it out. I noticed that sometimes, the onTokenRefresh does indeed fire. And I was wondering if it had something to do with how the flutter application is launched onto the emulator, in the sense that there is a race condition between when the token is generated and the listener attached.
To get the app to appear to start for the first time, I wiped the app data. Unfortunately this causes the flutter to automatically disconnect from the app which means I won't see the output of the print statement. So instead of trying to print when the token generation occurs, I assigned a value from the onTokenRefresh listener to a variable. I then updated a text widget with the value of the variable. And onTokenRefresh does indeed fire each time at start up if the app data has previously been wiped.

Flutter Drive tests crashes on OS pop-ip (e.g. notification consent)

I am using Flutter Drive for integration tests. The tests works well until app shows push notification consent. It seems Flutter Drive doesn't recognise the pop-up by iOS and crashes it there.
takeScreenshot(driver,"screenshots/01.login_screen_initial.png");
await driver.tap(tfUserId);
await driver.enterText('100111');
await driver.tap(tfPassword);
await driver.enterText('abc');
takeScreenshot(driver,"screenshots/02.login_screen_before_button_tap.png");
await driver.tap(loginButton);
After this button tap, the app asks for push notification consent. And the following code never gets executed.
takeScreenshot(driver,"screenshots/03.login_screen_just_after_button_tap.png");
await driver.waitForAbsent(btnHRDirectory,timeout: Duration(seconds: 360));
takeScreenshot(driver,"screenshots/04.dashboard_screen.png");
I get error as :
[VERBOSE-2:rasterizer.cc(307)] Last layer tree was null when screenshotting.
Detailed error is here.
The issue was somewhere in the script. driver.WaitFor along with timeout duration helps. No problem for the pop-up as Flutter Drive waits for the pop-up to vanish.