簡體   English   中英

Swift 編譯器錯誤 (Xcode):沒有這樣的模塊 'shared_preferences_ios' - Flutter

[英]Swift Compiler Error (Xcode): No such module 'shared_preferences_ios' - Flutter

筆記

  • 最近更新到 Xcode 版本 14.2 (14C18)
  • Flutter 3.7.0 • 通道穩定

問題

當為 ios 運行我的 flutter 應用程序時,出現此錯誤:

Xcode build done.                                           86.8s
Failed to build iOS app
Could not build the precompiled application for the device.
Swift Compiler Error (Xcode): No such module 'shared_preferences_ios'
/Users/tomasward/Desktop/fredi_new/ios/Runner/AppDelegate.swift:3:7

這是從AppDelegate.swift中的這段代碼派生的:

import UIKit
import Flutter
import awesome_notifications
import shared_preferences_ios //error here

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

            SwiftAwesomeNotificationsPlugin.setPluginRegistrantCallback { registry in
                SwiftAwesomeNotificationsPlugin.register(
                  with: registry.registrar(forPlugin: "io.flutter.plugins.awesomenotifications.AwesomeNotificationsPlugin")!)
                FLTSharedPreferencesPlugin.register(
                  with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
            }
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

此代碼是用於 ios 的awesome_notifications插件設置的樣板文件。請參見此處

我已經嘗試了一百萬個解決方案

  • 刪除 ios 構建並運行flutter create.
  • 運行pod install一百萬次,刪除 podfile 等。

似乎沒有解決方案是一致的。 我正在尋找從事Flutter項目而不是本地 ios 項目的人員的通用解決方案。

如果使用awesome_notifications的人可以給我一些見解,那也很棒

您現在可能已經知道, shared_preferences_iosshared_preferences版本 2.0.16 一起消失了。 因此,您所看到的事實意味着在此過程中某些內容沒有得到正確更新。

您之前使用的是哪個版本的 Flutter?

FWIW 我的AppDelegate.swift看起來很不一樣,特別是不包括特定插件的導入:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

另一方面,我沒有使用awesome_notifications 自您第一次集成以來,它的設置是否可能發生了變化,並且AppDelegate.swift修改不再相關? 而且,您使用的是最新版本的awesome_notifications嗎?

抱歉,我沒有立即直接的答案,但我希望這會有所幫助。

感謝此鏈接上的答案:

這是由 shared_preferences v2.0.16 創建的問題。 長話短說 - 包/模塊 shared_preferences_ios 已重命名為 shared_preferences_foundation。 解決此問題的最快方法是將 shared_preferences 降級到 2.0.15。

shared_preferences: ^2.0.15 # Before
shared_preferences: 2.0.15 # After

當您運行flutter pub get時,您將看到此日志:

< shared_preferences 2.0.15 (was 2.0.17) (2.0.17 available)
+ shared_preferences_ios 2.1.1
+ shared_preferences_macos 2.0.5

另外,請注意拼寫錯誤,因為shared_prefrences_ios (錯誤)與shared_preferences_ios (正確)非常相似。

對於最新版本,請使用此

shared_preferences_ios替換為shared_preferences_foundation

FLTSharedPreferencesPluginSharedPreferencesPlugin

因為這些是 flutter 內部的最新本地綁定。

AppDelegate.swift內容與新的更新解決方案

import UIKit
import Flutter
import awesome_notifications
import shared_preferences_foundation //replace this here

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)

      if #available(iOS 10.0, *) {
          UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
      }

      application.registerForRemoteNotifications()

            SwiftAwesomeNotificationsPlugin.setPluginRegistrantCallback { registry in
                SwiftAwesomeNotificationsPlugin.register(
                  with: registry.registrar(forPlugin: "io.flutter.plugins.awesomenotifications.AwesomeNotificationsPlugin")!)
                SharedPreferencesPlugin.register( // replace here
                  with: registry.registrar(forPlugin: "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin")!)
            }
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM