How to Access Variables from Another File

How to access a variable from another dart file . Flutter

Create a constructor onDateSelected so you can get a datetime when you select a date from date-picker.

class DateOfBirth extends StatefulWidget {
final Function onDateSelected;

DateOfBirth({this.onDateSelected});

@override
_DateOfBirthState createState() => _DateOfBirthState();
}

class _DateOfBirthState extends State<DateOfBirth> {
DateTime? dateTime;
String _text = '';

@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
border: Border.all(), borderRadius: BorderRadius.circular(5)),
width: double.infinity,
child: Padding(
padding: const EdgeInsets.only(left: 8, right: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text('Date Of Birth'),
Text(_text),
ElevatedButton(
onPressed: () async {
dateTime = await showDatePicker(
context: context,
initialDate: dateTime ?? DateTime.now(),
firstDate: DateTime(1900),
lastDate: DateTime.now(),
);
widget.onDateSelected(dateTime);
setState(() {
_text =
'${dateTime?.day}/${dateTime?.month}/${dateTime?.year}';
});
},
child: const Text('Date Of Birth'),
),
],
),
),
);
}
}

// You can get date like this.

DateOfBirth(
onDateSelected: (date) {
//
},
)

Python: Dynamically access variable from another file

You can use getattr() for this:

main_file.py

import config as cf

new_var = 'var' + str(1)
print(getattr(cf, new_var))

The attribute is here the variable of the imported module.



Related Topics



Leave a reply



Submit