I need a model object on two screens.
I have 2 questions:
Is passing a model object (from one screen to another) required if we are using GetX?
If yes, how do we do it?
return GestureDetector(
onTap: () {
Get.toNamed('/field_details/', arguments: fieldModel);
},
In main.dart:
GetPage(
name: '/field_details',
page: () => FieldDetailsScreen(//how do I get the model to be passed here, it won't be available in the main function),
),
You can use Get.arguments for that.
GetPage(
name: '/field_details',
page: () => FieldDetailsScreen(Get.arguments),
),
You will get your model here but you need to convert arguments into model by using your model's fromjosn method.
Related
How to get following result in flutter web app using Getx package
On tapping the container in intiating screen, I wish to pass parameters in following fashion ( 2 variable parameters and 1 fixed parameter i.e. product) which should open webpage with following url
https://myapp.local/variableParameter1/variableParameter2/product
Above link should be without # and also when user opens above link, should lead to (ProductScreen) with 2 parameters being passed as arguments
My code is structured in following fashion and works well on mobile devices.
Part of intiating screen
GestureDetector(
child: Container(
//some content inside
),
onTap: () {
Get.toNamed(
'/product/$variableParameter1/$variableParameter2',
);
},
)
Part of main.dart file
GetPage(
name: '/productscreen/variableparameter1/variableparameter1',
page: () => ProductScreen(),
binding: ProductScreenBinding(),
),
Part of controllers for ProductScreen
class ProductController extends GetxController {
var screenprm1 =Get.arguments[0].toString().obs;
var screenprm2 =Get.arguments[1].toString().obs;
}
How can I pass multiple rows of data from one screen to the second one without overloading the values of the first data passed?
onPressed: () {
setState(() {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => new orderpage(
'${widget.productName.toString()}',
'$total',
counter,
check_tandeef,
check_makwa,
sab8a)));
I think I understood your point, I'm not sure.
To pass values, you can use args in the Navigator and the use those values in your second screen.
https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments
Another way, would be using InheritedWidget class, so you can use the same instance of an object all across your widgets without being passing them by the context
https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html
I am assuming you mean you wan't to pass multiple sets of the same data to the next screen. You could do this by creating a class which holds all your params, and then pass a list to your Screen.
class myViewModel{
String param1;
String param2;
int param3;
bool param4;
myViewModel(this.param1,this.param2,this.param3,this.param4);
}
Create the list and fill it with instances of the Class:
listOfParams = List<myViewModel>();
Pass the List:
onPressed: () {
setState(() {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => new orderpage(
listOfParams
)));
1.FlatButton(
onPressed: () async {
var typedName=await Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return CityScreen();
},
),
);
print(typedName); //prints the value of cityName
}
2. FlatButton(
onPressed: () {
Navigator.pop(context, cityName);
},
The no.1 is coming from file loading_screen and no.2 is coming from city_screen. Can you anyone help me understand what is happening that when you pass a variable or anything in the pop? And when come that onPress method is still working because the the method Navigator.push has been already assigned to the variable but still that method Navigator.push is working when I pressed the button?Does that onPress doesn't care about the variable TypedName and just looks for the method Navigator.push?
If you use push method it will create the new screen over your current page and pop will remove last screen, you cannot pass name with both, you can check basics of navigation here, if you want to pass name to next page you should use something like pushnamed method and you can see the logic of passing name here. And for all methods you should check here.
What is the difference for these two variants? If the function _getCsvDocunent is just void type then what does it mean if we define onPressed like onPressed: () => _getCsvDocunent(), ?
FlatButton(
child: Text('Provide .csv data file'),
onPressed: _getCsvDocunent,
),
vs
FlatButton(
child: Text('Provide .csv data file'),
onPressed: () => _getCsvDocunent(),
),
In onPressed:_getCsvDocunent: onPressed gets an reference to _getCsvDocument passed to it. This way of passing a reference is only possible when the function to be passed is already defined and is compatible with onPressed.
In onPressed: () => _getCsvDocunent(): onPressed gets the value returned by
_getCsvDocunent after it is done executing.
In onPressed: () => _getCsvDocunent(): onPressed gets a reference to _getCsvDocument exactly similar to the case in the first bullet point, difference being, in this case a function reference (_getCsvDocument) is directly passed to onPressed instead of using an inline function to return a reference to _getCsvDocument.
I am using Provider as state manager. I have few widgets that collect data. One of my widget is button that should to complete saving of all data. It have onPressed () { ... } event. I would like to do some computation of data before saving.
I thought to do it in separate class:
class ResultProcessing
{
// here I need access to data from provider
}
Button:
Flexible(
child: Container(
child: RaisedButton(
child: Text("Save"),
onPressed: () {
// I need to pass data to ResultProcessing
},
)),
),
But the problem that I can't get access to data in Provider, because ResultProcessing class is not widget and have not context.
The class name data from which I need to grab from my processing class is Provider.of<AppState>(context).customer;
And in which moment I should to create instance of ResultProcessing? In onPressed event?
You don't, that's anti pattern.
Your objects are purposefully not accessible outside of the widget tree.
You should change your approach. Instead, use widgets to act like a bridge between your model and your providers.
A typical example is ProxyProvider and similar variants
You can do something like this.
Flexible(
child: Container(
child: RaisedButton(
child: Text("Save"),
onPressed: () {
DataClass data = Provider.of<DataClass>(context);
// Pass data to ResultProcessing
ResultProcessing.handle(data);
},
),
),
),
I solved it on a simple way. Add a required parameter to your class with a constructor and use it in the class, and then, send data from the widget where you have a build method(context) and access to the Provider.
class ResultProcessing {
String data; // add a parameter
ResultProcessing({required this.data}); // add the constructor
{
//use the data here as you want
}
}