Make parameter variable usable to setState in flutter - flutter

I want to get the value of latitude and longitude variables, so that I can pass them to the fetchAlbum() function inside initstate().
class MainContent extends StatefulWidget {
final double latitude;
final double longitude;
MainContent({Key key, #required this.latitude, #required this.longitude}) : super(key: key);
#override
_MainContentState createState() => _MainContentState();
}
class _MainContentState extends State<MainContent> {
bool isLoading = false;
Future<Album> futureAlbum;
#override
void initState() {
super.initState();
var a = 23.2599; // update the value of 'a' by using 'latitude' variable
var b = 77.4126; // update the value of 'a' by using 'longitude' variable
futureAlbum = fetchAlbum(a, b);
}

Do it like this:
var a = widget.latitude;
var b = widget.longitude;

Related

Pass value from Statefulwidget to Providerclass Flutter

i want to pass postid value from a statefulwidget to a function inside Providerclass, somebody please help
class CommentPage extends StatefulWidget {
final String postid;
final String postdescription;
CommentPage({
required this.postdescription,
Key? key,
required this.postid,
}) : super(key: key);
#override
State<CommentPage> createState() => _CommentPageState();
}
Providerclass
class CommentPageProvider extends ChangeNotifier {
static CommentPageProvider instance = CommentPageProvider();
List<HomeFeedDataModel> homeFeedDatacollection = [];
List<CommentsModelData> commentsDatacollection = [];
List<SingleUserPostModelData> singleuserdatacollection = [];
bool isLoadingdata = false;
bool isSingleUserdata = false;
bool isLikePressed = false;
bool isLoadCommentsPressed = false;
bool isLikeButtonPressed = false;
bool isCommentsDataloaded = false;
getCommentsData() async {
isCommentsDataloaded = true;
commentsData = await CommentService().getCommentsdata(post_id: );
notifyListeners();
}
so that i can fetch matching comments,
What's wrong with passing the post ID into the getCommentsData() as a parameter?
class CommentPageProvider extends ChangeNotifier {
static CommentPageProvider instance = CommentPageProvider();
List<HomeFeedDataModel> homeFeedDatacollection = [];
List<CommentsModelData> commentsDatacollection = [];
List<SingleUserPostModelData> singleuserdatacollection = [];
bool isLoadingdata = false;
bool isSingleUserdata = false;
bool isLikePressed = false;
bool isLoadCommentsPressed = false;
bool isLikeButtonPressed = false;
bool isCommentsDataloaded = false;
getCommentsData(String postId) async {
// I changed the name of this because there is no 'commentsData' field in your provider class
commentsDatacollection = await CommentService().getCommentsdata(post_id: postId);
isCommentsDataloaded = true;
notifyListeners();
}
By declaring your data collection as nullable (List<CommentsModelData> commentsDatacollection = [];), I think you could remove the boolean isCommentsDataloaded and simply check for a null value in your widget build method.
CommentPage
Here you just access the provider and pass through the post ID.
class CommentPage extends StatefulWidget {
final String postid;
final String postdescription;
CommentPage({
required this.postdescription,
Key? key,
required this.postid,
}) : super(key: key);
#override
State<CommentPage> createState() => _CommentPageState();
}
class _CommentPageState extends State<CommentPage> {
#override
Widget build(BuildContext context) {
final provider = Provider.of<CommentPageProvider>(context).getCommentsData(widget.postId);
}
}
Let me know if this helped!

how to use a data transfer from the parent widget?

Here is the child class that receives from its parent the index data that identifies the box to modify
class InformationPatient extends StatefulWidget {
final Patients patients;
final int index;
const InformationPatient(this.patients, this.index, {Key? key})
: super(key: key);
#override
State<InformationPatient> createState() => _InformationPatientState();
}
class _InformationPatientState extends State<InformationPatient> {
final int indexitem = 0;
late Box<Patients> boxPatient;
#override
void initState() {
super.initState();
boxPatient = Hive.box('Patient');
}
void _addNote(String newtitle, String newnote, String newconclusion) {
final newNOTE = Patients(
listOfNotes: [
ListNote(title: newtitle, note: newnote, conclusion: newconclusion)
],
);
boxPatient.put(indexitem, newNOTE);
Navigator.of(context).pop();
}
This way works but I don't have the index of the parent, I give it to him
final int indexitem = 0; <--- Works perfectly with my function patientbox.put() it receives the index
boxPatient.put(indexitem, newNOTE);
I would like to give indexbox the value of index but I don't know how to do it or if there is another way
final int indexitem = ??? <---- add the value of index
boxPatient.put(indexitem, newNOTE);
obviously I can't use index directly in the function boxPatient.put()
Thank you very much for your help
To read variables from the widget inside the state, you can simply refer to widget.<variable>. So in your example
boxPatient.put(widget.index, newNOTE);

How to display the middle element of a list in Flutter?

I have a similar widget for CupertinoPicker, but it was made by hand. I need to make the middle element of the list be selected by default, not the initial one as it is now. I've tried different options but haven't been able to do it yet. Please tell me how to implement this, I will be grateful for the help?
widget
class WheelSpeedPicker extends StatefulWidget {
final Function(dynamic) onValueChanged;
final int? startPosition;
final double itemSize;
final double? listHeight;
final int currentPosition;
final double? listWidth;
final FixedExtentScrollController? controller;
const WheelSpeedPicker({
Key? key,
required this.onValueChanged,
required this.currentPosition,
required this.itemSize,
this.startPosition,
this.listHeight,
this.controller,
this.listWidth,
}) : super(key: key);
#override
_WheelTimePickerState createState() {
return _WheelTimePickerState();
}
}
class _WheelTimePickerState extends State<WheelSpeedPicker> {
FixedExtentScrollController? fixedExtentScrollController;
int? currentPosition;
#override
void initState() {
super.initState();
currentPosition = widget.currentPosition;
fixedExtentScrollController = widget.controller ??
FixedExtentScrollController(initialItem: currentPosition ?? 0);
}
void _listener(int position) {
setState(() {
currentPosition = position;
});
widget.onValueChanged(currentPosition);
}
#override
void initState() {
super.initState();
// swap this
// currentPosition = widget.currentPosition;
// with this
currentPosition = (time.length / 2).floor();
fixedExtentScrollController = widget.controller ??
FixedExtentScrollController(initialItem: currentPosition ?? 0);
}
Other than that, I think your widget would be more useful if you added the items list to the widget parameters:
class WheelSpeedPicker extends StatefulWidget {
final Function(dynamic) onValueChanged;
final List<String> items;
final int? startPosition;
final double itemSize;
final double? listHeight;
final int currentPosition;
final double? listWidth;
final FixedExtentScrollController? controller;
const WheelSpeedPicker({
Key? key,
required this.items,
required this.onValueChanged,
required this.currentPosition,
required this.itemSize,
this.startPosition,
this.listHeight,
this.controller,
this.listWidth,
}) : super(key: key);
And then you'd have a more coherent set of parameters, because you already have a startPosition in the interface.
#override
void initState() {
super.initState();
currentPosition = widget.startPosition;
fixedExtentScrollController = widget.controller ??
FixedExtentScrollController(initialItem: startPosition ?? 0);
}

Accessing route parameters in Dart and flutter

I am sending 2 parameters restaurantDetails.rest_latitude and restaurantDetails.rest_longitude to a class to get the map details
BarMapWidget(restaurantDetails.rest_latitude, restaurantDetails.rest_longitude),
In the class I have a stateful widget
class BarMapWidget extends StatefulWidget {
String restLatitude;
String restLongitude;
BarMapWidget(this.restLatitude, this.restLongitude, {Key? key}) : super(key: key);
#override
_BarMapWidgetState createState() => _BarMapWidgetState();
}
I am passing a string so I know I have to make it a double
Here is the extended class,
class _BarMapWidgetState extends State<BarMapWidget> {
// Destination Longitude
final double _destLatitude = restLatitude;
final double _destLongitude = restLongitude;
void initState() {
// Add destination marker
_addMarker(
LatLng(_destLatitude, _destLongitude),
"destination",
// BitmapDescriptor.defaultMarkerWithHue(90),
BitmapDescriptor.defaultMarker
);
super.initState();
}
Which give me a Undefined name 'restLatitude'. and Undefined name 'restLongitude'.
How can I access the variable in BarMapWidget?
Your restLatitude and restLongitude on widget level, try using widget.variableName. Also, those aren't matching datatype.
If you need double convert BarMapWidget variable to double.
To initialize on state level you can trick with using late or use initState;
class BarMapWidget extends StatefulWidget {
final double restLatitude;
final double restLongitude;
const BarMapWidget(this.restLatitude, this.restLongitude, {Key? key})
: super(key: key);
#override
_BarMapWidgetState createState() => _BarMapWidgetState();
}
class _BarMapWidgetState extends State<BarMapWidget> {
// Destination Longitude
late final double _destLatitude = widget.restLatitude;
late final double _destLongitude = widget.restLongitude;
Just add in
widget.restLatitude

get data from constructor and pass it to variable

I want to get the data from the constructor and passes it to a variable.
I dont want it to be listed...
Class:
class ListaProjetos extends StatefulWidget {
ListaProjetos({Key key, this.title, this.jsonData}) : super(key: key);
static const String routeName = "/ListaProjetos";
final String title;
final List jsonData;
#override
_ListaProjetosState createState() => _ListaProjetosState();
}
I want to:
class _ListaProjetosState extends State<ListaProjetos> {
var message = widget.jsonData;
print(message);
//HERE I WANT TO PRINT ALL JSON DATA
}
Inside the class you need create a method and place the variables initialization, for example:
#override
void initState() {
var message = widget.jsonData;
print(message);
}
Or you can declare the var outside of the method to use it in others methods.
var message;
#override
void initState() {
message = widget.jsonData;
print(message);
}