0

Basic Issue: Need to access Class throughout flutter application.

Requirements: Do not want this to be a stateful widget that i access.

Simply want a class that I can access by importing on other pages. I am persisting a user login with a set of variables, and I don't want to have to push this object from page to page.

Would like to simply access the class values

I was unable to find any good example for this using a singleton or other solutions that gave me what I was looking for.

import 'package:myApp/models/user.dart';

MyClass.Username = "testUser"; //Set the username

String currentUser = MyClass.Username; // Get the username

//Here is the top of the User class - is this creating a class that will 
//only be defined once in the app, so if I set a value it will persist ?
class User {

static User _user = new User._internal();

factory User() {
  return _user;
}

User._internal();
//More stuff
....}

1 Answers1

0

If you want create a singleton class to access the same instance in any point of your application the source code shows an example of a singleton class. Read the comments.

class UserRepository {

  static UserRepository _instance = new UserRepository._internal();
  static get instance => _instance; // this is a get method that return _instance private class field
  // the name member is accessible directly like a public field
 String name;

 UserRepository._internal( ){
    name = " singleton name property";
 }

  // this is a instance method
  void myInstanceMethod(){
    print("hello my singleton");
  }
} //end of your singleton class

// usage in any point of your app.
UserRepository.instance.myInstanceMethod();

var myInstance = UserRepository.instance; // getting instance singleton reference
//accessing name class member
print("The name is:  ${myInstance.name}";)
myInstance.name = "user name";
print("Now the name is: ${myInstance.name}" );

//or you can access like this way too
UserRepository.instance.name = " User name";
print("The name is: ${UserRepository.instance.name}" );
Marcos Boaventura
  • 4,641
  • 1
  • 20
  • 27
  • [This](https://stackoverflow.com/questions/12649573/how-do-you-build-a-singleton-in-dart) shows about dart singletons. – Marcos Boaventura Feb 04 '19 at 22:08
  • Marcos, I read over the examples, but was unable to see a complete example showing the use of variables. Every example is basic like this, where it just calls a method, not demonstrating a value has been held from some prior call. Can you adjust the above code to include a String username ? – Jeremy Finch Feb 04 '19 at 22:25
  • Marcos, Can you clarify something about this implementation. On the top of your example we have a class myClass { } when referencing this on another .dart page. i need to do the following: #import .../myClass.dart; Does the implementation you have still work ? Should i be able to just put in print(UserRepository.instance.name); – Jeremy Finch Feb 04 '19 at 23:00
  • Yes will works. Assuming my source example... The class UserRepository has his own dart file called UserRepository.dart by example. If we want to use this class in another dart code file we always have to import "../UserRepository.dart" file and then you can just put lines like print(UserRepository.instance.name);. Since UserRepository is a singleton class your UserRepository instance will be the same in any point of your application and the data that this class holds will be the same too. Read a little bit about Singleton design pattern will open you mind about this. – Marcos Boaventura Feb 04 '19 at 23:13
  • Marcos, Thanks for the help- I think there was something wrong with an example I pulled off the flutter site, and it just compounded as I was working on it. Cleared things out, and have an http post working correctly with the custom class. Appreciate it ! – Jeremy Finch Feb 04 '19 at 23:17