Reputation: 1233
I learned SwiftUI before starting to learn flutter and I could not understand how to have something that is similar to Environment object in SwiftUI?
One solution I could think of is using singletons, But I am not sure if using singletons are a good flutter architecture solution.
For example in SwiftUI you would access data like so:
struct MyView: View {
@EnvironmentObject DataController data
var body: some View {
Text(data.title)
}
}
And the DataController
would look like this:
class DataController {
title = "Hello!"
}
On the other hand in flutter if I will use singleton it will look like this:
class MyWidget extends StateLessWidget {
@override
Widget build(BuildContext context) {
return Text(DataController.data.title);
}
}
And the DataController
in flutter is using a singleton
:
class DataController {
static DataController data = DataController();
title = "Hello!";
}
Is there a better way achieving this feature without passing arguments and values down the widget tree with provider package?
Upvotes: 5
Views: 480
Reputation: 1233
The best option I found is GetX package: https://pub.dev/packages/get
It is very convenient and handy, very similar to EnvironmentObject
Upvotes: 0
Reputation: 21
I'm actually trying to figure out the same problem, but I think that the provider Package might be the right choice. There are plenty of good tutorials and a complete installation guide on pub.dev
Upvotes: 2