I wrote this code to get access to my global scope more comfortable.
extension DepContextExtension on BuildContext {
IGlobalDependency get global => read<IGlobalDependency>();
}
I had this code to use my global dependensy
final globalScope = context.read<IGlobalDependency>();
I change it to this
final globalScope = context.global();
but i got error, that method global isn't defined for the type BuildContext. What i did wrong and how i can fix this issue?
I get globalSopce inside WidgetModel Factory from library Elementary
MainAppScreenWidgetModel mainAppScreenWidgetModelFactory(BuildContext context) {
final globalScope = context.global();
return MainAppScreenWidgetModel(
MainAppScreenModel(
globalScope.baseBloc,
),
);
}
this factory i put inside my Widget like this
class MainAppScreenWidget extends ElementaryWidget<MainAppScreenWidgetModel> {
const MainAppScreenWidget({
Key? key,
WidgetModelFactory wmFactory = mainAppScreenWidgetModelFactory,
}) : super(key: key, wmFactory);
You've declared a property so to access it just use its name:
final globalScope = context.global;
Related
This is my Notifier:
class Counter extends Notifier<int> {
final int initial;
Counter(this.initial);
#override
int build() => initial;
}
I need to pass initial value to it, but I'm unable to do that using the family modifier anymore.
// Error
final counterProvider = NotifierProvider.family<Counter, int, int>((initial) {
// How to get the initial value to pass here?
return Counter(initial);
});
The syntax for using family/autoDispose using Notifier/AsyncNotifier is different. You're supposed to change the inherited type
So instead of:
final provider = NotifierProvider(MyNotifier.new);
class MyNotifier extends Notifier<Value> {
With family you should do:
final provider = NotifierProvider.family(MyNotifier.new);
class MyNotifier extends FamilyNotifier<Value, Param> {
And the same reasoning applies with autoDispose.
I would like to have a class that contains a list of another class and be able to update it using Riverpod/Hooks. I'm trying to figure out the best syntax or even way to approach the issue.
I'm starting with a StateProvider to make it simpler and thought to use the List method .add() to add to the list but I'm getting the error: "The method 'add' can't be unconditionally invoked because the receiver can be 'null'." or null check operator used on null value (when I add the !)- so I think I'm not writing it correctly and I'm sure it has to do with the Riverpod or even Dart syntax.
Any suggestions or resources would be appreciated. Thanks in advance.
So for example:
Session({this.title, this.sessionThings})
String? title;
List<SessionThings> sessionThings;
}
class SessionThings{
SessionThings({this.title, this.time})
String? title;
double? time;
}
ref.read(buildExSessionProvider.notifier).state = Session()..sessionThings.add(SessionThings(title: 'something', time: 20);
using:
hooks_riverpod: ^1.0.3
flutter_hooks: ^0.18.3
Do not access state outside a notifier. Something like this could work:
class SessionStateNotifier extends StateNotifier<Session?> {
SessionStateNotifier() : super(null);
void create(String title) => state = Session(title: title);
void add(SessionThings things) =>
state = Session(title: state?.title, sessionThings: [...state?.sessionThings ?? [], things]);
}
Use with hooks
class MyApp extends HookConsumerWidget {
#override
Widget build(BuildContext context, WidgetRef ref) {
final state = useState(0);
final Session? session = ref.watch(sessionProvider);
final SessionStateNotifier sessionStateNotifier = ref.watch(sessionProvider.notifier);
return <widget>
}
}
``
I use dart 2.13.3 . In my flutter app I can't initialized Set variable without required annotation.I want to build constructor without final Set<V>initialSelectedValues;
or without required, because from another call this parameter is just optional. But show error and suggest me to add required annotation at the front of final Set<V>initialSelectedValues;. How to I change and solve this error?
class MultiSelectDialog<V> extends StatefulWidget {
MultiSelectDialog({required this.items,this.initialSelectedValues});
final List<MultiSelectDialogItem<V>> items;
final Set<V>initialSelectedValues;
#override
State<StatefulWidget> createState() => _MultiSelectDialogState<V>();
}
final selectedValues = await showDialog<Set<int>>(
context: context,
builder: (BuildContext context) {
return MultiSelectDialog(
items: items,
//initialSelectedValues: [1,2].toSet(),
);
},
);
Now, I can solve this. Change to default parameter with const .
MultiSelectDialog({required this.items,this.initialSelectedValues = const {}});
final List<MultiSelectDialogItem<V>> items;
final Set<V> initialSelectedValues;
Every final variable must have a value, if you want to set them through the constructor you have to put "required" in front of every parameter.
MultiSelectDialog({
required this.items,
required this.initialSelectedValues });
I am facing a few strange issues with Flutter. I do have very little knowledge about Flutter. I am learning it.
class ViewOtherProfile extends StatefulWidget {
final String userName;
final int points;
const ViewOtherProfile({
#required this.userName,
#required this.points,
});
You can see i am getting userName and Points data as argument.
I want to print this argument in the page. Like this
class _ViewOtherProfileState extends State<ViewOtherProfile> {
..........
void initState(){
print(points);
deviceInfo();
super.initState();
print(userName);
]);
}
............
Now problem is i am getting error.
Undefined name 'userName'.
Try correcting the name to one that is defined, or defining the name.
Any reason why i am getting this error and how i can resolve it.
Thanks to #jamesdlin
I tried to put it like this
print(ViewOtherProfile.userName);
but now i am getting another error.
Instance member 'userName' can't be accessed using static access.
There are two main types of widgets in Flutter. StatelessWidget and StatefullWidget. A StatelessWidget is built only one when the UI builds and is never rebuilt. Meanwhile, a StatefulWidget can be rebuilt every time if a call for setState is made.
Hence, for a StatefulWiget, there is a need to track the state of the class by extending the main class with a State class.
You have to note that the scope of variables in those two types of widgets can vary. For example...
class ExampleStateless extends StatelessWidget {
final String userName;
final int points;
const ExampleStateless({
#required this.userName,
#required this.points,
});
#override
Widget build(BuildContext context){
print(userName); print(points);
return Something();
}
}
Note that for the stateful widget, there are two classes and each class has its scope. The superclass ExampleStateful can share its scope to its subclass _ExampleStatefulState via the widget
class ExampleStateful extends StatefulWidget {
final String userName;
final int points;
Static final string id = "exampleState";
const ExampleStatefull({
#required this.userName,
#required this.points,
});
// scope 1
#override
_ExampleStatefulState createState() => _ExampleStatefulState();
}
class _ExampleStatefulState extends State<ExampleStateful>{
// scope 2
final String userName2 = null;
#override
void initState(){
super.initState();
print(widget.userName);
print(userName2);
print(ExampleStateful.id); // you can do this only if variable is static.
}
}
What is in scope 1 can be accessed in scope 2 via the widget properties. eg print(widget.userName); instead of print(userName);
I am creating a widget in flutter like below:
class CatalogWidget extends StatelessWidget {
final String errorMsg;
final Function receiveAgainCallbackyyyyyyyyyy;
const CatalogWidget({this.errorMsg, this.receiveAgainCallbackyyyyyyyyyy});
As you can see in named constructor receiveAgainCallbackyyyyyyyyyy is long name now my question is here?
How can i use long name argument but when i want to initialize this constructor i see short one like this:
CatalogWidget(errorMsg: "test", callback: function)
I know this is mistake:
const CatalogWidget({this.errorMsg, callback})
: this(receiveAgainCallbackyyyyyyyyyy);
But i don't remember what is the correct way
Like this?
class CatalogWidget extends StatelessWidget {
final String errorMsg;
final Function receiveAgainCallbackyyyyyyyyyy;
const CatalogWidget({this.errorMsg, Function callback})
: receiveAgainCallbackyyyyyyyyyy = callback;
}
You can do something like this:
const CatalogWidget({this.errorMsg, Function callback})
: this.receiveAgainCallbackyyyyyyyyyy = callback;