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

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,
);

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).

How to use a Firebase emulator demo project in Flutter?

I have a Flutter project where I use Firebase Authentication. I'd like to test it locally. Here is how Firebase local emulator is started:
firebase emulators:start --project demo-test --only auth
Here is Firebase initialization from main.dart:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: FirebaseOptions(
apiKey: 'any',
appId: 'any',
messagingSenderId: 'any',
projectId: 'demo-test',
));
//...
runApp(...);
}
Everything starts smoothly without errors. The app works. However when I try to sign up a new user:
firebase_auth.FirebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
I receive an error:
[firebase_auth/unknown] com.google.firebase.FirebaseException: An internal error has occurred. [ API key not valid. Please pass a valid API key. ]
I suspect that I need to provide a proper API key for demo-test project but where can I find it? Or may be I can provide one when starting the emulator? I couldn't find answer in Google docs.
To clarify things. The app works fine when I use options of my real Firebase project. The problem comes only with a so-called Demo project.
Make sure to call useAuthEmulator in your code before using the authentication service. The Flutter code for that may be missing from the docs (I just filed an issue to get it added there), but the API to call can be found here.
I'm don't exactly recall how I addressed that cleartext issue last time I encountered it, but am quite sure it was by following some top search results including the android:usesCleartextTraffic="true" that you mention.

Why firebase not working with flutter web?

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.

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.