Flutter - Use both GlobalKey and ValueKey for a custom widget - flutter

I have a custom widget class which I need to render it into a bitmap icon using BitmapDescriptor, so I need to define it's GlobalKey.
GlobalKey<TheWidgetState> globalkey = GlobalKey<TheWidgetState>();
#override
Widget build(BuildContext context) {... TheWidget(globalkey) ...}
...
//Somewhere. Has nothing to do with the issue
BitmapDescriptor.fromBytes((await (await (globalkey.currentContext?.findRenderObject() as RenderRepaintBoundary).toImage()).toByteData(format: ui.ImageByteFormat.png))!.buffer.asUint8List());
...
Before doing that, I also need to update the state of the widget before render it like in above code, without changing it's position on the tree. So I also need to define it's ValueKey to do that.
I'm still confused about the purpose of every other key types in Flutter. But as for now I need both the purpose of GlobalKey and ValueKey for my widget. Since both need to be defined with variable name key, I have neither an idea on how to set the widget with both keys nor an answer which key serve both purposes that I need.
Greatly appreciate the help.

Related

Passing builder atribute to a isolate widget on dart

I am trying to pass a index attribute from an api to a image widget which show the network image, i get the undefined identifier error
I think you have missed to pass the index from MoviesList() widget to MovieCard Widget

How to update widget tree from another widget class - Flutter

I am facing problem to re-render the page when a variable changes in one class/widget (both parent and child widgets are stateful classes).
Here is how it goes:
I have three buttons in one class which changes a variable state (foodCategory).
int foodCategory = 0;
// down in the elevated button body - i am updating the variable
setState(() {
foodCategory = 1;});
While in the other widget, i am using this variable to perform certain actions:
for (var item in foodItems.values.elementAt(foodCategory))
GestureDetector(........ and so on...
However in the second snippet, the widget dose not know if there has been a change and it is not calling the buildcontext again...
I am not sure how to overcome this problem. I have tried valuelistenablebuilder but in vain. Maybe i dont know how to use it in the loop (i am using foodcategory as an int (iterator)).
it happned to be that i was sing valuelistenable builder in a wrong way.
It is easy. Just mark the variable and changes as valueNotifier. In my case, i needed to mark foodCategory as a valueNotifer.
Than, i needed to wrap the Widget (in my case column widget) as ValueListenableBuilder. This solved my issue.

Flutter - How to avoid repeating StreamBuilder?

in my app I often need a list of objects that I get with 3 StreamBuilders. I want to avoid repeating those StreamBuilders every time.
So I tried to do a Stateful Widget that contains my 3 StreamBuilders with a Widget child argument returned by the last StreamBuilder.
But it doesn't work. The datas are not communicated to the child Widget.
What is the good way to do it please?
Click on StreamBuilder()
Then right click => select Refactor form the options => Click on Extract Flutter Widget
Now create a file with any name like streambuilder_helper.dart
Cut and paste the Extracted Widget in this file. Make sure to import material.dart
Now suppose you want to use this anywhere in your app just import the file streambuilder_helper.dart
While creating this flutter widget if you want to use this anywhere make sure to create several finals like
final myStream;
and then suppose the name of the flutter widget you defined is myStreamBuilder() then you have to create
myStreamBuilder({}); inside the flutter widget
and define this myStream final in it
myStreamBuilder({this.myStream});
and finally replace the hard coded code with your final in the StreamBuilder.

what is the benefit behind provider or BLoC pattern?

We have a lot of State management solutions like providers and BLoC pattern. But, why do we need them?. why can't I create a file called 'data.dart' and import this file(data.dart) wherever i need, and make changes to the variables and objects in this file(data.dart)? Does this pattern has any downsides?
State management solutions are needed for datas that changes.
Imagine having data.dart file that has a variable
String text = 'abc';
And you have a Text() widget called TextA that takes in text variable as input.
This widget would initially display abc
now you have this function called
void changeText(){
text = 'cba';
}
how would this function tell TextA to rebuild because the value of text has already been changed?
of course you can use setState((){}); as long as the function is part of TextA,
but what if the Widget is ButtonA?
similar to StatefulWidget you can change the value using setState() to rebuild the widgets but what if you want a button in child widget that change the value in parent widget ? here you need to use provider to get full control.
Note: this is as example but it has multiple uses. you can use it with Firebase auth or cloud Firestore.

What Type of GlobalKey is needed for a TextFormField?

Usually key of a Widget is var key = GlobalKey<WidgetState> but TextFormFieldState is private. Since I have only one text form, I am avoiding to wrap it in Form. Can it be done?
GlobalKey<FormFieldState>()