I am creating an app in Flutter and I would like to store and reference a user's name in future dart files. I am currently working on the account setup, and the first dart file contains a textfield where the user must enter in their name. I want to be able to show that user inputted name in a different file, but I am unsure of how to do so. Any help is greatly appreciated!
Asked
Active
Viewed 200 times
0
-
Welcome to SO! Does [this answer](https://stackoverflow.com/a/72697422/2828341) solves your problem? – lepsch Jun 21 '22 at 17:37
-
@lepsch This did not work for me because I already have a controller defined. I use a controller to make an elevated buton become active once the user enters their name. With this method, I don't think I am able to define more than 1 controller – Ta-Ty Jun 21 '22 at 18:59
-
@YeasinSheikh Hello, this does not quite make sense to me. I read the attached URL and it does not really specify how to store the textfield information. The previous suggestion was to use a controller, but I already have one specified for the textfield box. I also cannot use .pushNamed because I am using the PageTransitions package – Ta-Ty Jun 21 '22 at 19:06
-
If the problem is just to keep the app state somewhere maybe this would help you: [Simple app state management](https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple). Also, [follow the tutorial](https://docs.flutter.dev/development/data-and-backend/state-mgmt/intro) to get the basics of state management. – lepsch Jun 21 '22 at 19:21
-
Can you include [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example) – Md. Yeasin Sheikh Jun 21 '22 at 20:19
1 Answers
1
Update: I figured it out! A linked post gave me a hint. (How can I navigate between 2 classes, one of them requires passing data? in flutter)
Since I already had a controller defined for a different application, I couldn't use TextController(). Instead, I created an empty string and stored the textfield input in it. I then saved it as an argument in RouteSettings(), then used an if/else statement and ModalRoute() to pull the user-inputted value. Thank you everyone for your help!
I've attached chunks of my code :)
class _NamePageState extends State<NamePage> {
String _nickname = "";
}
// Creation of TextField
TextField(
onChanged: (val) {
setState(() {
_nickname = val;
});
},
)
// Elevated Button info
ElevatedButton(onPressed: isButtonActive
? () {
Navigator.push(context,
PageTransition(
type: PageTransitionType.rightToLeftJoined,
duration: const Duration(milliseconds: 650),
child: const SelectionScreen(),
childCurrent: widget,
settings: RouteSettings(
arguments: _nickname)
),);
// Second dart file calling TextField parameter
Widget build(BuildContext) {
double _height = MediaQuery.of(context).size.height;
final data = ModalRoute.of(context)!.settings;
String retrieveString;
if (data.arguments == null) {
retrieveString = "empty";
} else {
retrieveString = data.arguments as String;
}
// Text being shown
Text('Hello there $retrieveString!)

Ta-Ty
- 77
- 1
- 15