6

I wish to know, how to hold data which I am using in multiple screens. Should we use the Singleton design pattern in a flutter?

Suppose I made login module using BLoC pattern like here https://github.com/adamjohnlea/Flutter-Login-With-BLoC-Pattern.

Now if for every request to the server, I need an email and password to be sent.

Provider code:

import 'package:flutter/material.dart';
import 'bloc.dart';

class Provider extends InheritedWidget {

  final bloc = new Bloc();

  Provider({Key key, Widget child})
    : super(key: key, child: child);

  bool updateShouldNotify(_) => true;

  static Bloc of(BuildContext context) {
    return (context.inheritFromWidgetOfExactType(Provider) as Provider).bloc;
  }

}

Bloc code:

import 'validators.dart';
import 'package:rxdart/rxdart.dart';

class Bloc extends Object with Validators {
  final _email = BehaviorSubject<String>();
  final _password = BehaviorSubject<String>();

  // retrieve data from stream
  Stream<String> get email    => _email.stream.transform(validateEmail);
  Stream<String> get password => _password.stream.transform(validatePassword);
  Stream<bool>   get submitValid => Observable.combineLatest2(email, password, (e, p) => true);

  // add data to stream
  Function(String) get changeEmail    => _email.sink.add;
  Function(String) get changePassword => _password.sink.add;

  submit() {
    final validEmail    = _email.value;
    final validPassword = _password.value;

    print('$validEmail and $validPassword');
  }

  dispose() {
    _email.close();
    _password.close();
  }
}

1. Should I make singleton class eg. AccountManager, where I will store the user details and get details wherever I need? or Should I use below Provider to get the email & password anywhere in the app?

2. Is it recommended to hold data in Singleton classes?

Ankur Prakash
  • 1,417
  • 4
  • 18
  • 31
  • 1
    you can use shared_preferences or sqflite packages – diegoveloper Jan 21 '19 at 07:05
  • I think shared_preferences is to store user preferences. It is not recommended for passing data between controllers. Regarding sqflite, yes that is an option but an app will make a continuous request for different screens then app need to fetch user (in this case), in other cases, it might be an array of items I wish to share. My concern was to know how flutter recommends data handling in such cases. – Ankur Prakash Jan 21 '19 at 08:02

3 Answers3

0

you can use shared_preferences, Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing a persistent store for simple data. Data is persisted to disk asynchronously. Neither platform can guarantee that writes will be persisted to disk after returning and this plugin must not be used for storing critical data. shared_preferences

satish
  • 1,304
  • 3
  • 13
  • 33
0

It's better to store data in cache for getting the data anywhere and any time in the app. For that shared_preferences package is good, but It's not fast as it uses Native code to fetch shared_preference instance. It's better to use Hive Instead of shared_preferences, as it's faster, secure ,written in pure dart and easy to setup.

Jay Dangar
  • 3,271
  • 1
  • 16
  • 35
0

I think using a singleton or just static data would be good options. Local data that is not going to change should NOT be stored in state management. One of the biggest problems in mobile development today is devs incorrectly using state management. It is not meant to just store data and should not be used that way.

EJ Thayer
  • 151
  • 2
  • 3