Reputation: 13
I am connecting to an api server. After logging in I receive an access and a refresh token. Also user's name, surname, permission list etc. I always use these properties to display / allow something for the user to do. Also with all the requests I should send access token. I am managing logging in process with BLOC pattern but I don't know where to store all my commonly used data. (like this user data) Maybe in a singleton class? So I can get the data from that class before sending a request.
Do you have any recommendation for it? Because I have no idea.
Upvotes: 0
Views: 1205
Reputation: 6876
After the some discussion in the comments, I'm adding the answer also.
The BLoC classes not for just handling the logic, also keeping the data for memory.
Inside StatefulWidget
and StatelessWidget
, yes you can acces the bloc provider with context
but for access between bloc, you can simply use singleton
. So how to create singleton?
There two basic approaches:
class Bloc{
....//some logic and variable
}
final bloc = Bloc(); //Singleton object for bloc, it's static because
// it's outside of the Class and it can be directlry accessible for
// any file that imports this file
or
class Bloc{
....//some logic and variable
Bloc._internal(); // private constructor
static final bloc = Bloc._internal(); // static object which will sent
// through public factory
factory Bloc() => bloc; // when you use this constructor through your
// application, you'll always get same instance
}
So, using a bloc inside yet another bloc would be like this:
class BlocA{ //this is for second method
final blocB = BlocB();
blocB.method();...
}
For the first method up above use the object, that's all.
Upvotes: 2
Reputation: 758
You can create a singleton class and keep your commonly used data there or create a Bloc
class and initialize it in the beginning of your app, it will be like a singleton.
If you already using Bloc
pattern you can do like this:
void main() {
runApp(BlocProvider<UserBloc>(
builder: (_, bloc) => bloc ?? UserBloc(),
onDispose: (_, bloc) => bloc.dispose(),
child: MyApp(),
));
}
I don't know how your Bloc
pattern is implemented, but if you doesn't familiar with this BlocProvider
implementation, I can give you a BlocProvider
class that I'm using for my apps.
import 'package:flutter/material.dart';
abstract class BlocBase {
void dispose();
}
class Provider<B> extends InheritedWidget {
final B bloc;
const Provider({
Key key,
this.bloc,
Widget child,
}): super(key: key, child: child);
static B of<B>(BuildContext context){
final type = _typeOf<Provider<B>>();
Provider<B> provider = context.inheritFromWidgetOfExactType(type);
return provider.bloc;
}
static Type _typeOf<B>() => B;
@override
bool updateShouldNotify(Provider<B> oldWidget) => oldWidget.bloc != bloc;
}
class BlocProvider<B> extends StatefulWidget{
final void Function(BuildContext context, B bloc) onDispose;
final B Function(BuildContext context, B bloc) builder;
final Widget child;
BlocProvider({
Key key,
@required this.child,
@required this.builder,
@required this.onDispose,
}) : super(key:key);
@override
_BlocProviderState<B> createState() => _BlocProviderState<B>();
}
class _BlocProviderState<B> extends State<BlocProvider<B>>{
B bloc;
@override
void initState(){
if(widget.builder != null){
bloc = widget.builder(context, bloc);
}
super.initState();
}
@override
void dispose(){
if(widget.onDispose != null){
widget.onDispose(context, bloc);
}
super.dispose();
}
@override
Widget build(BuildContext context){
return Provider(
bloc: bloc,
child: widget.child,
);
}
}
After initialization you will get your main Bloc
class through injections.
Like this:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final userBloc = Provider.of<UserBloc>(context);
return MaterialApp(
home: HomePage(userBloc: userBloc)
}
}
So, HomePage(userBloc: userBloc)
this is how you will inject your Bloc
class through your widgets tree.
Another way to keep the data like login information, is to use shared_preferences package. It is a store for simple data, which you can have a quick access. It will save your data in the device.
Hope this will help.
Upvotes: 1