I have a HomePage, from where I want to pass my coordinates:
child: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MapScreen(historyItem.latLong)),
);
},
child: Icon(Icons.place),
)
I want to pass them to my MapScreen, which is stateful. However, I want these coordinates to be an optional argument with initial values so that I can decide the behavior of MapScreen depending on my arguments passed:
class MapScreen extends StatefulWidget {
const MapScreen({Key? key}) : super(key: key);
final AppLatLong kPoint = const AppLatLong(lat: 0, long: 0);
@override
State<MapScreen> createState() => _MapScreenState();
}
How do I do that? Is it possible?
Thanks everyone in advance!
2
Answers
make this Mapscreen constructor like this:
and pass the parameter like this:
let me know if this is working for you.
Now your kPoint is optional, if you pass a kPoint you will have this, if not, you have your default value
const AppLatLong(lat: 0, long: 0)
.My example is a named param, so you would’ve to use like this
But if you want a positioned param, declare the param outside the curly braces:
…