【问题标题】:Flutter : A build function returned null and NoSuchMethodError: The method '[]' was called on nullFlutter:构建函数返回 null 和 NoSuchMethodError:在 null 上调用了方法“[]”
【发布时间】:2019-07-09 09:54:34
【问题描述】:

我为用户配置文件数据构建了一个表单,但为什么总是在我的调试控制台中显示这样的消息:

I/flutter (31833):抛出了另一个异常:构建函数 返回空值。 I/flutter (31833):抛出了另一个异常: NoSuchMethodError:在 null 上调用了方法“[]”。我/颤动 (31833):引发了另一个异常:NoSuchMethodError:方法 '[]' 在 null 上被调用。

这是我的完整代码:

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: () {

                    }),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您应该检查您的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(),
    

    【讨论】:

      猜你喜欢
      • 2021-09-29
      • 2020-06-07
      • 2019-03-10
      • 2020-05-23
      • 1970-01-01
      • 2020-04-09
      • 2021-11-30
      • 2020-08-11
      • 2020-05-05
      相关资源
      最近更新 更多