Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional cast from 'AppDelegate' to 'UNUserNotificationCenterDelegate' always succeeds (Flutter)

Tags:

ios

swift

flutter

I've tried to connect iPhone to build my flutter project, but this error occurs :

Conditional cast from 'AppDelegate' to 'UNUserNotificationCenterDelegate' always succeeds

I guess it might be related with local notifications, but have no idea why the error occurs and how to fix it.

AppDelegate.swift

import UIKit
import Flutter
import UserNotifications

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

like image 275
RyanIdea Avatar asked Aug 04 '20 15:08

RyanIdea


1 Answers

What @Larme mentioned in the comments is correct. There's no need for self (FlutterAppDelegate) to be casted since AppDelegate inherits the FlutterAppDelegate class. The code should be

UNUserNotificationCenter.current().delegate = self

If you're using flutter_local_notifications plugin, you can also try removing ? as demonstrated in the sample provided in their docs.

like image 94
Omatt Avatar answered Nov 15 '22 08:11

Omatt