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?