0

I want to access the shared preferences at the application startup and want to use this same object across the entire app by passing it to the classes. I am getting the following error:

The argument type 'Future' can't be assigned to the parameter type 'SharedPreferences'.

main.dart

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:application/layouts/ScreenOne.dart';
import 'package:application/layouts/ScreenTwo.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  sharedPreferences() async {
    return await SharedPreferences.getInstance();
  }

  final preferences = SharedPreferences.getInstance();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyApp',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: (preferences.getInt("login") == 1 ? ScreenOne(preferences) : ScreenTwo(preferences)),
    );
  }

}

I am unable to resolve this error. Is there anything I am doing wrong or missing? Thanks!!!

Stack Overflow
  • 1
  • 5
  • 23
  • 51

1 Answers1

1

First of all, you defined function sharedPreferences() but did not use it later in the code - simply remove it.

Furthermore, based on the documentation SharedPreferences.getInstance() returns Future<SharedPreferences> and not SharedPreferences, hence you get the following error. You can resolve the issue by getting the SharedPreferences instance in the main method and then using constructor injection to provide the preferences object to the MyApp:

Future<void> main() async { // <-- Notice the updated return type and async
  final preferences = await SharedPreferences.getInstance(); // <-- Get SharedPreferences instance
  
  runApp(
    MyApp(preferences: preferences), // <-- Inject (pass) SharedPreferences object to MyApp
  );
}

class MyApp extends StatelessWidget {
  final SharedPreferences preferences;
  
  const MyApp({
    required this.preferences,
  })

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyApp',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: (preferences.getInt("login") == 1 ? ScreenOne(preferences) : ScreenTwo(preferences)),
    );
  }
}
mkobuolys
  • 4,499
  • 1
  • 11
  • 27
  • Thank you for the help, so, this `preferences`, I will also be able to pass and use across the app classes, right? – Stack Overflow Mar 27 '22 at 20:42
  • Not really, you should specifically provide the `preferences` to your Widget tree by using [InheritedWidget](https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html) or third-party packages like [provider](https://pub.dev/packages/provider). Also, there is some info on [what to do when someone answers your question](https://stackoverflow.com/help/someone-answers) – mkobuolys Mar 27 '22 at 20:44
  • Could you please help me to know how to create the playground for receiving the same `preferences` in class also? Or how to get the `preferences` in the other screen? – Stack Overflow Mar 27 '22 at 20:44
  • This is a completely different question than the original one. If you have follow-up questions, do not start a thread in the comments and please create a new question in Stack Overflow - more people could help you this way. – mkobuolys Mar 27 '22 at 20:50