Flutter: Can I use Provider.of<Class> in normal class components? - flutter

to propagate properties or methods down the tree in Flutter widgets, I usually use the Provider package with Provider.of<someClass>(context).doSomething(). Now, I also have normal class based components that do something, e.g File handling or so.
Can I somehow inject or use my Provider class in normal class components, too?
I assume its not possible the usual way, because Provider needs a context, but is there another way?

Related

Where is Flutter's Navigator declared?

When creating a Flutter application,
we use a Navigator to move back and forth between screens.
In my application, I have some class instances that
I want to share with all child widgets in the hierarchy.
An example is a Data Base driver class (stateful)
which holds all functionality of reading/writing to the actual database.
Currently - the 'main' function is initializing it,
then the resulting object is passed through all
constructors of the participating widgets.
How do I make it behave like the Navigator
so it would be available in the global scope
without importing it or passing it through a chain of constructors?
I would gladly accept any other approaches,
and still, if you can also address the exact question - I would be grateful :)
I opened the code of Navigator, Stateless and Stateful widgets,
I tried to google for the source of that Navigator instance
but came up with nothing helpful.
Have you tried using InheritedWidget?
From the docs:
Base class for widgets that efficiently propagate information down the tree.
Create DataBaseDriver class as inherited Widget class, access it in its child class using context as DataBaseDriver.of(context)

why do we need to extend statefull or stateless classes rather create an object of them and use it in flutter?

I am new to flutter, I wonder why we extend Statefull or stateless classes to create our own widgets, why not creating the objects of the instead. Now many others might say it has build function that needs to be overridden but I guess it can also be done inside the object. Please give me an explanation.
flutter has lifecycle, widget will appear after build
https://i.stack.imgur.com/94idE.png
Both Stateful and Stateless classes are abstracts.
So that, to use methods such as initState in Stateful or "build" in both kind of widgets you need extend either Stateful or Stateless (exactly "extends" keyword, if you use "implements" keyword, you will be forced to implement all of the methods described in abstract).
More about:
StatefulWidget
StatelessWidget

With flutter, how to add a Provider later to the application?

I have a flutter application which uses a MultiProvider widget at the root of the widgets tree.
The problem is, I need to add a ChangeNotifierProvider to the MultiProvider widget, but later because I can't create it at the beginning (well I could but it makes more sense to do it later *).
How am I supposed to do?
(* for more details about why I would like to create the ChangeNotifierProvider later: The associated ChangeNotifier represents a bluetooth connection that will not be available at the start of the application.)
In these situations I create Provider with object which have some initial values and later I change initial values to actual.

What is the best form to pass data between screen with GetX?

I am new on Flutter, and I am using GetX.
I want to do a Stepper for registering, and I want to pass one object between 4 screens and fill some data on each screen.
What is the best method to do that? I was thinking to put a UserModel on a controller and pass it between screens.
Thank you so much.
Generally you don't need to manually pass around data to different pages when using most state management solutions. You store the relevant data in a single instance of the object that you access from anywhere.
class StepController extends GetxController {
// any data in this class is accessible from anywhere and doesn't
// need to be manually passed to any pages
}
Use the variables you create in the GetX class for the Steps and then from anywhere in the app you find the controller with
final controller = Get.find<StepController>();
Just make sure you initialize once it at some point with
Get.put(StepController());
I haven't use GetX but MobX(+provider). In your case, I think dependency injection is best way to share store between screens, you can consume the stores everywhere, so you dont need to pass data to other screen.

GWT - Keep widget instance between modules

I'm developing an application consisting of 3 modules.
The users can start a web chat (implemented as a widget) and then navigate through the modules. I would like the web chat widget to be kept during the navigation.
I've added a static field in the widget containing the current instance, but it is only persisted while I remain in the same module. If I change module the widget is re-created by scratch.
Is there a way to define an object with application scope?
A better solution (than keeping global state) would be to use Dependency Injection and inject your widget to all relevant views.
Using gin it'd look like this:
#Singleton
public class CommonWidget {
}
public class View1 {
#Inject
public View1(CommonWidget widget) {
}
}
public class View2 {
#Inject
public View2(CommonWidget widget) {
}
}
Now, widget in View1 and View2 point to the same widget.
The problem is that you have to be careful when using this widget - remember that you can only add a widget once to the DOM, so before adding it in View2, you have to remove it from View1.
I think that instead sharing the widget this way, you should either share the messages between two chat widgets or better yet, move the chat widget "above" the views (either literally or figuratively), so that there's only one instance.
Of course, there are more steps to setting up gin, but you can read about them in the docs.
You can create any widget in your entry point class (#onModuleLoad), and then reference/call this widget from any activity that needs it. However, I would only recommend this solution in two cases:
A widget is a container for the entire application or part of the application (e.g. top menu, main panel) that is always visible on a screen, so you don't add/remove it from UI - you simply call it to change its state.
A widget is a popup panel or similar (e.g. confirm dialog, error notification, etc.), so reusing it in different activities/views does not cause any problems.
You may want to take a look at Activities and Places design pattern. It supports most typical navigation scenarios, where you move from one Activity/View to another within the same app (container) widget.
UPDATE:
This is a quote from GWT docs about the use of multiple modules:
If you have multiple GWT modules in your application, there are two
ways to approach loading them.
Compile each module separately and include each module with a separate
tag in your HTML host page.
Create a top level module XML
definition that includes all the modules you want to include. Compile
the top level module to create a single set of JavaScript output.
The
first approach may seem the easiest and most obvious. However, the
second approach will lead to much better end-user performance. The
problem with loading multiple modules is that each module has to be
downloaded separately by the end-user's browser. In addition, each
module will contain redundant copies of GWT library code and could
possibly conflict with each other during event handling. The second
approach is strongly recommended.