Search code examples
flutterdartsharedpreferencesflutter-dependencies

When I add shared_preferences dependency in pubspec.yaml my project doesn't run


when I add Shared_Preferences dependency my project doesn't run

Version : last version 2.2.0

This is the error message:

FAILURE: Build failed with an exception.

What went wrong: A problem occurred configuring project ':shared_preferences_android'. Could not resolve all files for configuration ':shared_preferences_android:classpath'. Could not find gradle-7.2.2.jar (com.android.tools.build:gradle:7.2.2). Failed to notify project evaluation listener. Could not get unknown property 'android' for project ':shared_preferences_android' of

I added WidgetsFlutterBinding.ensureInitialized() in the main function. When I remove the dependency my problem did get solved, but I need this dependency for save token as string

When I remove the dependency my problem did get solved.


Solution

  • You have to make sure your gradle version is updated

    location => android/gradle/wrapper/gradle-wrapper.properties

    distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
    

    location => android/build.gradle

    dependencies { classpath 'com.android.tools.build:gradle:7.4.2' }
    

    after that, you can add your dependencies from the terminal

    flutter clean 
    
    flutter pub add shared_preferences
    flutter pub get