skip to Main Content

In the run console

Launching libmain.dart on Android SDK built for x86 in debug mode...
Running Gradle task 'assembleDebug'...
Parameter format not correct -
√  Built buildappoutputsflutter-apkapp-debug.apk.
Installing buildappoutputsflutter-apkapp-debug.apk...
Debug service listening on ws://127.0.0.1:6400/pR_lm1UHzmA=/ws
Syncing files to device Android SDK built for x86...
E/flutter ( 5558): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null return value., null, null)
E/flutter ( 5558): #0      FirebaseCoreHostApi.optionsFromResource (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:248:7)
E/flutter ( 5558): <asynchronous suspension>
E/flutter ( 5558): #1      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:89:25)
E/flutter ( 5558): <asynchronous suspension>
E/flutter ( 5558): #2      Firebase.initializeApp (package:firebase_core/src/firebase.dart:43:31)
E/flutter ( 5558): <asynchronous suspension>
E/flutter ( 5558): #3      main (package:ncc_apps/main.dart:8:3)
E/flutter ( 5558): <asynchronous suspension>
E/flutter ( 5558):       
void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}    

`when i remove await .. its working. what should i do?

firebase integrate properly.`

2

Answers


  1. First, you need to initialize Firebase on your project using Firebase CLI.

    You can start with firebase init, after Firebase setup you will get firebase_options.dart which contains your project configuration, your main function will be like

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,// --<<
      );
    }
    
    Login or Signup to reply.
  2. Add these dependencies in your pubspec.yaml file

      firebase_core: ^2.21.0
    

    then inside main.dart file

    void main() async {
      //initialize firebase
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      runApp(const MyApp());
    }
    

    if you are setting firebase for push notifications add these 2 also in pubspec.yaml

      firebase_messaging: ^14.7.3
      flutter_local_notifications: ^16.1.0
    

    Then finally, Install FlutterFire CLI from https://firebase.google.com/docs/cli#setup_update_cli

    then open terminal and use command below and follow instruction

    flutterfire configure
    

    this will generate firebase_options.dart file.

    Also to implement push notifications you can add this code (create new file firebase_api.dart)

    import 'dart:convert';
    import 'package:firebase_messaging/firebase_messaging.dart';
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    
    final _localNotifications = FlutterLocalNotificationsPlugin();
    
    //this function is used for handleing background notifications for Android,
    Future<void> handleBackgroudMessage(RemoteMessage message) async {
      print('Title: ${message.notification?.title}');
      print('Body: ${message.notification?.body}');
      print('PayLoad: ${message.data}');
    }
    
    final androidchannel = const AndroidNotificationChannel(
        'high_importance_channel', 'High Importance Notifications',
        description: 'This channel is used for important notification ',
        importance: Importance.defaultImportance);
    // Function for Sending to selected page from notifications
    void handleMessage(RemoteMessage? message) {
      if (message == null) {
        return;
      }
    }
    
    Future initLocalNotification() async {
      const android = AndroidInitializationSettings('@drawable/ic_launcher');
      const iOS = DarwinInitializationSettings();
      const settings = InitializationSettings(android: android, iOS: iOS);
      await _localNotifications.initialize(settings,
          onDidReceiveNotificationResponse: (payload) {
        final message = RemoteMessage.fromMap(jsonDecode(payload as String));
        handleMessage(message);
      });
      final platform = _localNotifications.resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>();
      await platform?.createNotificationChannel(androidchannel);
    }
    
    Future initNotifications() async {
      await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
          alert: true, badge: true, sound: true);
    
      FirebaseMessaging.instance.getInitialMessage().then(handleMessage);
      FirebaseMessaging.onMessageOpenedApp.listen(handleMessage);
      FirebaseMessaging.onBackgroundMessage(handleBackgroudMessage);
      FirebaseMessaging.onMessage.listen((message) {
        final notification = message.notification;
        if (notification == null) {
          return;
        }
    
        _localNotifications.show(
          notification.hashCode,
          notification.title,
          notification.body,
          NotificationDetails(
            android: AndroidNotificationDetails(
              androidchannel.id,
              androidchannel.name,
              channelDescription: androidchannel.description,
            ),
          ),
          payload: jsonEncode(message.toMap()),
        );
      });
    }
    
    //function for firebase and asking for persmission of notifications,
    void setupPushNotifications() async {
      final fcm = FirebaseMessaging.instance;
    
      await fcm.requestPermission();
      final token = await fcm.getToken();
      print("Token : $token");
    
      fcm.subscribeToTopic("ALL");
    
      FirebaseMessaging.onBackgroundMessage(handleBackgroudMessage);
      initNotifications();
      initLocalNotification();
    }
    

    and in main.dart file
    make you widget stateful (if not) and then use init method for using this code,

     @override
      void initState() {
        // initializing push notifications service (ask for permissions)
        setupPushNotifications();
        super.initState();
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search