Coder Social home page Coder Social logo

Comments (2)

harriseldon avatar harriseldon commented on July 17, 2024

I did get it to not have errors with a few modifications.

  1. within the app.dart, presolveUsing: LocalStorageServiceShared.getInstanceReady
    Notice that it is the actual function name. When the build_runner runs, it will covert it to the async function call
  2. Null safety requires that you declare _instance as LocalStorageServiceShared?, but locator cannot resolve this to LocalStorageServiceShared. Therefore I changed your getInstanceReady function definition to not include the ? in type definition and changed the return to also do an explicit cast to LocalStorageServiceShared.
dependencies: [
  Presolve(
      classType: LocalStorageServiceShared,
      presolveUsing: LocalStorageServiceShared.getInstanceReady),
]
import 'package:shared_preferences/shared_preferences.dart';

class LocalStorageServiceShared {
  static LocalStorageServiceShared? _instance;
  static SharedPreferences? _preferences;
  
  // Changed from Future<LocalStorageServiceShared?> to Future<LocalStorageServiceShared>
  static Future<LocalStorageServiceShared> getInstanceReady() async {
    if (_instance == null) {
      _instance = LocalStorageServiceShared();
    }
    if (_preferences == null) {
      _preferences = await SharedPreferences.getInstance();
    }
    // Added explicit cast
    return (_instance as LocalStorageServiceShared);
  }
}

The resulting app.locator.dart now looks as follows

Future setupLocator() async {
  final localStorageServiceShared =
      await LocalStorageServiceShared.getInstanceReady();
  locator.registerSingleton(localStorageServiceShared);
}

Notice how the getInstanceReady was properly converted to the function getInstanceReady()

from flutter-tutorials.

JasmeetSingh30 avatar JasmeetSingh30 commented on July 17, 2024

Awesome it worked. Thank you soooo much dude!!!!!! Now i know that i lack knowledge on null safety and OOP. Can you help me get a guide on these and anything more which will help me understand these basic concepts , i have just started with dart and flutter.

from flutter-tutorials.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.