我为用户配置文件数据构建了一个表单,但是为什么总是在调试控制台中像这样显示消息:
I / flutter(31833):引发了另一个异常:构建函数 返回null。 I / flutter(31833):引发了另一个异常: NoSuchMethodError:方法'[]'在null上调用。我/扑 (31833):引发了另一个异常:NoSuchMethodError:方法 在空值上调用了[[]]。
这是我的完整代码:
import 'package:flutter/material.dart';
class EditProfile extends StatefulWidget {
final String value;
EditProfile({Key key, this.value}) : super(key: key);
@override
_EditProfileState createState() => _EditProfileState();
}
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
String selectSex;
class _EditProfileState extends State<EditProfile> {
@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
appBar: new AppBar(
title: new Text('Edit Profile'),
),
body: new Padding(
padding: const EdgeInsets.all(10.0),
child: new SingleChildScrollView(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0),
child: new TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(), hintText: 'First Name'),
),
),
new Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: new TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Birth Date'),
),
),
new Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: new Container(
decoration: new BoxDecoration(
border: new Border.all(color: Colors.black),
borderRadius: new BorderRadius.all(Radius.circular(4.0)),
),
height: 55.0,
width: double.infinity,
child: new DropdownButtonHideUnderline(
child: new DropdownButton<String>(
hint: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Text('Sex'),
),
value: selectSex,
items: <String>['Man', 'Woman']
.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Padding(
padding: const EdgeInsets.all(10.0),
child: new Text(value),
),
);
}).toList(),
onChanged: (value) {
print(value);
setState(() {
selectSex = value;
});
},
),
),
),
),
new Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: new TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(), hintText: ' Handphone'),
),
),
new Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: new TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(), hintText: 'Status'),
),
),
new SizedBox(
width: double.infinity,
child: new RaisedButton(
child: new Text(
'Save',
style: TextStyle(color: Colors.white),
),
color: Colors.orange,
onPressed: () {
}),
),
],
),
),
),
);
}
}
答案 0 :(得分:0)
您应该检查您的items
值:
items: <String>['Man', 'Woman']
.map((value) { // remove the `String` attribute
return new DropdownMenuItem<String>(
value: value,
child: new Padding(
padding: const EdgeInsets.all(10.0),
child: new Text(value),
),
);
}).toList(),