【发布时间】:2021-08-15 08:42:13
【问题描述】:
我正在尝试通过 Flutter 框架为带有 firebase 的应用程序构建用户设置菜单,但每当我尝试打开设置表单时,它都会告诉我之前分配给“name”的 name 属性现在设置为 null .
class SettingsFormTwo extends StatefulWidget {
@override
_SettingsFormState createState() => _SettingsFormState();
}
class _SettingsFormState extends State<SettingsFormTwo> {
final _formKey = GlobalKey<FormState>();
final List<String> sugars = ['0', '1', '2', '3', '4'];
final List<int> strengths = [100, 200, 300, 400, 500, 600, 700, 800, 900];
// form values
String _currentName;
String _currentSugars;
int _currentStrength;
@override
Widget build(BuildContext context) {
Player player = Provider.of<Player>(context);
return StreamBuilder<UserData>(
stream: DatabaseService(uid: player.uid).userData,
builder: (context, snapshot) {
UserData userData = snapshot.data;
return Form(
key: _formKey,
child: Column(
children: <Widget>[
const Text(
'Update your brew settings.',
style: TextStyle(fontSize: 18.0),
),
SizedBox(height: 20.0),
TextFormField(
initialValue: userData.name, // This is where it says the error is at
decoration: textDecoration,
validator: (val) => val.isEmpty ? 'Please enter a name' : null,
onChanged: (val) => setState(() => _currentName = val),
),
],
),
);
}
);
}
}
这是我收到的错误消息的 sn-p。
The following NoSuchMethodError was thrown building StreamBuilder<UserData>(dirty, state: _StreamBuilderBaseState<UserData, AsyncSnapshot<UserData>>#169ee):
The getter 'name' was called on null.
Receiver: null
Tried calling: name
【问题讨论】: