【问题标题】:Flutter - The method 'map' was called on nullFlutter - 在 null 上调用了方法“map”
【发布时间】:2018-06-26 07:03:15
【问题描述】:

我在尝试使用对象模型将我的 api JSON 中的元素添加到 DropdownMenuItem 时收到以下错误

这是错误:

The method 'map' was called on null.
Receiver: null
Tried calling: map<DropdownMenuItem<Provinces>>(Closure: (Provinces) => DropdownMenuItem<Provinces>)

这是我的飞镖代码:

import 'dart:async';

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:sj/utils/constants.dart';
import 'package:http/http.dart' as http;
import 'package:sj/models/master/provinces.dart';

class IdCardFormPage extends StatefulWidget {
  @override
  _IdCardFormPageState createState() => new _IdCardFormPageState();
}

class _IdCardFormPageState extends State<IdCardFormPage> {

  List<Provinces> listProvinces;
  Provinces _selectedProvince;

  Future<List<Provinces>> getProvinceList() async {
    //final response = await http.get("${APIConstants.API_BASE_URL}api/masters/provincesList.php");
    final response = await http.get("url");

    listProvinces = parseProvinces(response.body);

    return parseProvinces(response.body);
  }

  List<Provinces> parseProvinces(String responseBody) {
    final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();

    return parsed.map<Provinces>((json) => Provinces.fromJson(json)).toList();
  }


  @override
  void initState() {

    getProvinceList();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text("MASUK"),),
      body: _provinceContainer(),
    );
  }

  Widget _provinceContainer(){
    return new Container(
        child: new InputDecorator(
          decoration: InputDecoration(
              suffixIcon: new Icon(
                Icons.account_balance,
                color: Colors.pink,
              ),
              labelText: LoanEntryField.PD_BANKNAME, labelStyle: TextStyle(fontSize: 16.0)
          ),
          isEmpty: _selectedProvince == null,
          child: new DropdownButtonHideUnderline(
            child: new DropdownButton<Provinces>(
              value: _selectedProvince,
              isDense: true,
              onChanged: (Provinces newValue) {
                setState(() {
                  _selectedProvince = newValue;
                });
              },
              items: listProvinces.map((Provinces value) {
                return new DropdownMenuItem<Provinces>(
                  value: value,
                  child: new Text(value.name, style: new TextStyle(fontSize: 16.0),),
                );
              }).toList(),
            ),
          ),
        ),
        margin: EdgeInsets.only(bottom: 10.0));
  }

}
class Provinces{
  String id;
  String name;

  Provinces({this.id, this.name});

  factory Provinces.fromJson(Map<String, dynamic> json) {
    return Provinces(
      id: json['id'] as String,
      name: json['name'] as String,
    );
  }

}

如何解决这个问题?

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    listProvicesnull 时传递一个空容器:

    items: listProvices?.map((Provinces value) {
                return new DropdownMenuItem<Provinces>(
                  value: value,
                  child: new Text(value.name, style: new TextStyle(fontSize: 16.0),),
                );
              })?.toList() ?? [],
    

    【讨论】:

    • 一个简单的listProvinces?.map(...) ?? [] 应该足够了
    • 它不包括空箱。
    • 但是items 需要一个列表。所以空列表是有效的;而Container 无法编译:P
    • 感谢您的提示 - 已修复。
    • 不错的解决方案,而是一个空容器,我通过了 CircularProgressIndicator 并且效果很好。谢谢
    【解决方案2】:

    我有一个类似的问题,通过将它包装在一个检查列表是否有超过 0 个项目的条件中来解决。

    【讨论】:

      【解决方案3】:

      在尝试将未赋值列表传递给 DorpdownMenu 小部件的 items 属性时遇到了同样的问题。

      错误消息The method 'map' was called on null。表示您已在 null 上调用了索引 [ ] 运算符。

      因此,在使用List 之前,请为其分配一个值 List 或初始化 List。

      我确实初始化了列表,它得到了修复,下面的代码

      List<Provinces> listProvinces = [];
      

      在容器不编译时它仍然可以工作

      【讨论】:

        【解决方案4】:

        检查你所有的拼写错误,有时可能会出现拼写错误。

        【讨论】:

          猜你喜欢
          • 2020-11-06
          • 2019-09-23
          • 2020-06-04
          • 2021-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-23
          • 2020-01-25
          相关资源
          最近更新 更多