So, I wanted to include firebase into my Flutter App, it works fine for android but when I start it on my ios emulator it will always give me this error
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.
Usually this means you've attempted to use a Firebase service before calling Firebase.initializeApp.
View the documentation for more information: https://firebase.flutter.dev/docs/overview#initialization
I have the google-service file in the right directory and I followed the tutorial and edited my main() to:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
Has anybody the same problem with IOS or did I do anything wrong?
So for everybody else getting this error I figured it out myself, for IOS, the line
Firebase.initializeApp()
it needs an option:
Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform)
for that follow the instructions on this link:
https://firebase.flutter.dev/docs/cli
hope that helps anyone
Install Firebase CLI if it is not installed in your system.
Next, install the FlutterFire CLI or by running the following command:dart pub global activate flutterfire_cli. Once installed, the flutterfire command will be globally available.
Run flutterfire configure in your flutter project folder in terminal,
Select a Firebase project to configure your Flutter application with,
Select/Insert android app id and bundle id you want to use as and when it asks you.
and get firebase_options.dart.
In main.dart file import firebase_options.dart.
In void main() use following code.
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isIOS) {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
} else {
await Firebase.initializeApp();
}
runApp(
const MaterialApp(
home: MyApp(),
),
);
I got same problem. My Android application is working fine but when using IOS application it's throw this type of exception. you can use #Viki's answer and if its not working for you make sure you have added GoogleServices-info.plist file in your ios project.
If it's not solve your issue then use this code
if (Platform.isIOS) {
await Firebase.initializeApp(
options: FirebaseOptions(
apiKey: "your api key Found in GoogleService-info.plist",
appId: "Your app id found in Firebase",
messagingSenderId: "Your Sender id found in Firebase",
projectId: "Your Project id found in Firebase"));
} else {
await Firebase.initializeApp();
}
I have faced the same issue in my flutter application when run on xcode/ ios.
Here is how I solved.
Download GoogleService-Info.plist from firebase account
Add Files to Runner like below and choose GoogleService-Info.plist from download folder
Re-Run the project, It will work
In my case, I haven't added the code lines according to the instructions from firebase. The simple fix is ββto follow the instructions from the firebase doc from start to finish.
In my case, I forgot to add await because it returns Future.
Previously:
Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
Then
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
Here is the full function to copy paste.
Convert the main function to:
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
}
Related
package.json-
"#react-native-firebase/app": "8.3.1",
"#react-native-firebase/auth": "8.3.3",
"#react-native-firebase/firestore": "7.5.3",
Initialisation-
export const initGuestChatConnection = async () => {
// DEFAULT app will always be there
if (firebase.apps.length < 2) {
await firebase.initializeApp(
GUEST_CHAT_FIREBASE_CONFIG,
GUEST_CHAT_FIREBASE_CONFIG.app_name
);
}
};
Wrapper method to get secondary app instance-
export const getGuestChatFirebaseInstance = () => {
return firebase.app(GUEST_CHAT_FIREBASE_CONFIG.app_name);
};
When we try to use auth (custom signin), we get following error-
Exception '*** -[__NSDictionaryM setObject:forKeyedSubscript:]: key cannot be nil' was thrown while invoking addAuthStateListener on target RNFBAuthModule with params (
"GUEST_CHAT"
)
Our signin method-
export const signInGuestChatFirebaseInstance = token => {
return getGuestChatFirebaseInstance()
.auth()
.signInWithCustomToken(token);
};
We thought that problem is with authentication, so we skipped it and tried to directly access Firestore (by modifying Firestore Rules) but then we started getting following error-
Exception 'FirebaseApp instance may not be nil. Use FirebaseApp.app() if you'd like to use the default FirebaseApp instance.' was thrown while invoking collectionOnSnapshot on target RNFBFirestoreCollectionModule with params (
"GUEST_CHAT",
This is working fine on Android. These methods are working on ios as well for DEFAULT Firebase app but for some reason it's crashing for secondary app.
Create iOS app for the secondary project in order to make it work. Since on adding iOS app it provides GoogleService-info.plist, so extract all config related info from GoogleService-info.plist and use it to create config for initialising second app. This will make secondary app work on iOS!
In order to implement inAppPurchase using expo am facing some issues.am referring https://docs.expo.io/versions/latest/sdk/in-app-purchases.i followed all steps mentioned in the document.I ejected my expo for this.In my App.js i write a code to Connects to the app store.
App.js
import * as InAppPurchases from "expo-in-app-purchases";
export default class App extends React.Component {
componentDidMount = async() => {
const history = await InAppPurchases.connectAsync();
console.log("history", history); //Here it is getting undefined...
}
............
}
am getting this error in ios.am building the solution using xcode and run in real device.
what should i do?
In the firebase database, I have only one object. And it has only one value.
In android, everything is working fine. In iOS, the app crashes on 'on' method.
Below message appears in the red screen in iOS:
undefined is not a function (this._database.native.on...)
Here is the code that I am using in componentDidMount.
componentDidMount() {
database()
.ref('/checkPlatform')
.on('value', snapshot => {
let firebaseData = snapshot.val()
this.setState({ isAndroidDisable: firebaseData.isPlatformEnabled })
});
}
Instead of the on method, I've also tried once(). But no luck.
Any help will be appreciated.
If you are getting "this._database.native.on is not a function" on ios try -
run the pod install - after adding firebase new service(database/auth ...)
terminate the metro bundler and restart it using "npx react-native start"
run "npx react-native run-ios"
This will create a new build and the error should be gone.
Ref: https://github.com/invertase/react-native-firebase/issues/3379
I'm implementing chat app and have to use transaction to synchronize messages of users. But, there are fatal error which terminates application abruptly. How can I deal with?
I searched many materials for this, but there was no solution. Github issue is related on this and I already wrote issue on this.
Future<void> sendMessage(String content,String receiver,String chatRoomID) async {
DocumentReference doc = Firestore.instance
.collection(firestoreMessageCollection)
.document(chatRoomID)
.collection(chatRoomID)
.document(DateTime.now().millisecondsSinceEpoch.toString());
await Firestore.instance.runTransaction((tx) async{
await tx.set(doc,{
firestoreChatFromField: sl.get<CurrentUser>().uid,
firestoreChatToField: receiver,
firestoreChatTimestampField: DateTime.now().millisecondsSinceEpoch.toString(),
firestoreChatContentField: content
});
});
}
I expected right result which transaction does normally, but below error is occurred and application was terminated.
E/AndroidRuntime( 1719): Caused by: java.lang.IllegalArgumentException: Provided document reference is from a different Firestore instance.
Even if I'm using same firestore instance, it is occurred continuously. Are there anyone who solved this horrible problem?
It was related on the firestore setting. When you initialize firestore settings, jus use Firestore.instance. If you use other instance, it occurs my error
I was also getting the same issue. It was fixed when I updated document reference code. Can you try changing document reference
from:
DocumentReference doc = Firestore.instance
.collection(firestoreMessageCollection)
.document(chatRoomID)
.collection(chatRoomID)
.document(DateTime.now().millisecondsSinceEpoch.toString());
to:
DocumentReference doc = Firestore.instance.document('$firestoreMessageCollection/$chatRoomID/$chatRoomID/${DateTime.now().millisecondsSinceEpoch.toString()}');
I also updated dependencies versions. In my case these are:
firebase_core: ^0.4.0+6
firebase_auth: 0.11.1+7
cloud_firestore: 0.12.5+2
firebase_storage: ^3.0.2
firebase_messaging: ^5.0.4
firebase_crashlytics: ^0.0.4+8
Here incase of android firebase crash can be implemented in APPLICATION class like.. below example
(...here, if android app is crashed this overrriden method uncaughtException(Thread thread, Throwable e) is called where we report crash to firebase...)
So, I want to know if there is any better way to implement firebase crash in iOS swift 3 like this.
/** onCreate method of MainApplication.java */
#Override
public void onCreate() {
super.onCreate();
reportFirebaseCrash();
}
/** Report FirebaseCrash Exception if application crashed */
private void reportFirebaseCrash() {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread thread, Throwable e) {
AppUtils.showLog(TAG, "reportFirebaseCrash");
FirebaseCrash.report(e);
}
});
}
Highly recommend you check out the Firebase docs β they're quite well done and should give you all the help you need to get started.
In particular, the simplest way to get up and running is to use the crash reporting pod:
pod 'Firebase/Crash'
Then you need to configure a FIRApp shared instance. Once that's set up (or if you have already), you need to configure your build system to upload crash reports. The docs link includes all the necessary details to do it, but tl;dr, you need to create a new Run Script build phase:
# Replace this with the GOOGLE_APP_ID from your GoogleService-Info.plist file
GOOGLE_APP_ID=1:my:app:id
# Replace the /Path/To/ServiceAccount.json with the path to the key you just downloaded
"${PODS_ROOT}"/FirebaseCrash/upload-sym "/Path/To/ServiceAccount.json"
Firebase also supports Bitcode in order to gather crash data from production users.