Nathan Bird
Nathan Bird

Reputation: 932

Flutter Xcode build failure after adding 1st-party dependency

While building an app using Android Studio with the Flutter SDK I decided to get the shared_preferences plugin and play around with it. Soon after, I noticed that although the app was running fine on my Android device, it wouldn't build to run on Simulator. In an attempt to figure out a solution, I tried running it directly from Xcode but again, it's failing there too.

Following the errors, I opened the GeneratedPluginRegistrant.m file where the editor is just saying "shared_preferences/SharedPreferencesPlugin.h file not found"

Running flutter doctor in the terminal doesn't give any useful info and running pod install doesn't help.

I've also tried adding FlutterFire plugins and they all produce the same result.

Any help is appreciated!

Upvotes: 2

Views: 2492

Answers (3)

Maryna K.
Maryna K.

Reputation: 921

I also got this problem for many days in my Flutter app for iOS and have done almost everything explained here and in github.

My solution was (1) after "flutter upgrade" call "flutter run" (also you have to run simulator or attach device for a correct work of this command) and (2) after successful building of an app through the command line, mentioned above, close it and build in a regular way with Xcode via Product -> Build.

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657338

I think this is a known issue https://github.com/flutter/flutter/issues/15099#issuecomment-372375566

To apply the fix in #15437 to a Swift-based Flutter project created before the fix landed, add [these lines] to ios/Podfile.

Which I think means https://github.com/mravn-google/flutter/blob/e0c73220a6f69d341ce436244212277d83bc189b/packages/flutter_tools/templates/cocoapods/Podfile-swift#L70-L73

 # workaround for https://github.com/CocoaPods/CocoaPods/issues/7463
 target.headers_build_phase.files.each do |file|
   file.settings = { 'ATTRIBUTES' => ['Public'] }
 end

Upvotes: 4

Pravin Raj
Pravin Raj

Reputation: 3599

The concept of Shared Preference is android specific and not available in iOS. If you want to save something, I recommend you use Method Channel(Platform Channel) in Flutter to send it to the native layer and save it.

For iOS use userDefaults to save your data.

Additional info:

How to save data using Shared Preference?

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long

editor.commit(); // Don't forget to commit when your changes are done.

To retrive data

// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean

How to save data using userDefaults?

NSString *valueToSave = @"someValue";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave 
forKey:@"preferenceName"];
[[NSUserDefaults standardUserDefaults] synchronize];

To retrieve saved data

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"preferenceName"];

UPDATE :

There is a flutter plugin which does this for us.

Add this to your pubspec.yaml file

dependencies:
shared_preferences: "^0.4.0"

And run in command line in your project root directory.

$ flutter packages get

Now in your Dart code you can use,

import 'package:shared_preferences/shared_preferences.dart';

Upvotes: 0

Related Questions