Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - The method 'map' was called on null

Tags:

flutter

dart

I'm receiving the following error while trying to add elements from my api JSON using Object Model to DropdownMenuItem

here is the error :

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

here is my dart code :

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,
    );
  }

}

How to solve this issue?

like image 972
Antonius Suharmono Avatar asked Jun 26 '18 07:06

Antonius Suharmono


2 Answers

Pass an empty container while listProvices is null:

items: listProvices?.map((Provinces value) {
            return new DropdownMenuItem<Provinces>(
              value: value,
              child: new Text(value.name, style: new TextStyle(fontSize: 16.0),),
            );
          })?.toList() ?? [],
like image 153
Günter Zöchbauer Avatar answered Sep 19 '22 00:09

Günter Zöchbauer


Got the same issue while trying to pass a without assign value list to the items property of the DorpdownMenu widget.

The error message The method 'map' was called on null. is indicating you that you've called the index [ ] operator on null.

So before using the List, assign a some value to it List or Initialize the List.

to I did Initialize the List and it got fixed, following code

List<Provinces> listProvinces = [];

It will still work while containerer doesn't compile

like image 36
Paresh Mangukiya Avatar answered Sep 17 '22 00:09

Paresh Mangukiya