Reputation: 792
I m trying to print the current value of my TextField using Globalkey's currentstate as shown:
GlobalkeyName.currentState.value
when i do this inside a Statefull widget am i getting no issue , but when i try to do the same inside a showModalBottomSheet
I am getting a error
The getter 'currentState' isn't defined for the class 'Key'.
P.S: I Know how to use TextEditingController Don't Suggest that.
Complete Source code below
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: MyStatelessWidget(),
),
);
}
}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
MyStatelessWidget({Key key}) : super(key: key);
Key _coffeeformkey=GlobalKey<FormState>();
Key _nametextfield=GlobalKey<FormFieldState>();
Key _sugartextfield=GlobalKey<FormFieldState>();
@override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
child: const Text('showModalBottomSheet'),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
child: Form(
key: _coffeeformkey,
child: Center(
heightFactor: 2,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
// crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
//Text("${brews.documents}"),
FractionallySizedBox(
widthFactor: 0.7,
child: TextFormField(
key: _nametextfield,
// initialValue: usertoselect1.data['name'],
//controller: namecontroller,
decoration: InputDecoration(hintText: "Enter Email"),
),
),
SizedBox(
height: 30,
),
FractionallySizedBox(
widthFactor: .7,
child: TextFormField(
key: _sugartextfield,
//controller: sugarcontroller,
//initialValue:await usertoselect1.data['sugar'],
decoration: InputDecoration(hintText: "Enter Password"),
),
),
SizedBox(
height: 30,
),
RaisedButton(
onPressed:(){
//final DatabaseService _database= DatabaseService();
print("yyfdyyyyyyyyyyyyyy");
print(_sugartextfield.currentState.value);
//print(_sugartextfield.r);
//print(_sugartextfield.runtimeType);
//_database.updateUser(_nametextfield.currentState.value, sugarcontroller.text, 400);
},
child: Text("Update"),
color: Colors.brown,
)
],
),
),
),
);
},
);
},
),
);
}
}
Upvotes: 0
Views: 2938
Reputation: 14043
Fix the error by changing the declaration of your key to the code below:
// use a global key instead
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
NOTE: The difference between a Key
and GloablKey
is explained below:
KEY
A Key is an identifier for Widgets, Elements and SemanticsNodes.
GLOBAL KEY
A key that is unique across the entire app.
To read more about keys, check the link below:
Upvotes: 1
Reputation: 792
well Apparently declaring the key like this:
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
instead of like this worked for me:
Key _sugartextfield=GlobalKey<FormFieldState>();
Upvotes: 0