Home > Blockchain >  How to fix this kind of error Unhandled Exception: NoSuchMethodError: The method 'map' was
How to fix this kind of error Unhandled Exception: NoSuchMethodError: The method 'map' was

Time:09-06

I'm trying to make a simple ListView with GetX but it gives me this error when starting the app "Unhandled Exception: NoSuchMethodError: The method 'map' was called on null.", I'm new to flutter and dart, that's why I'm starting with the "easiest" and for work reasons they ask me to add GetX

Home

class HomePage extends GetView<HomeController> {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    //  final homeController = Get.put(HomeController());
    var title = "HomePage";
    return Scaffold(
      
      body: Obx(() {
        HomeController controller = Get.find<HomeController>();
        return controller.regionList.isEmpty
            ? const Center(
                child: Text('No hay regiones'),
              )
            : ListView.builder(
                itemCount: controller.regionList.length,
                itemBuilder: (context, index) => ListTile(
                        title: Text(
                      controller.regionList[index].name,
                    )));
      }),
    );
  }
}

Controller

class HomeController extends GetxController {

  //late Regiones model;
  var regionList = <Regiones>[].obs;

  Future<List<Regiones>> getRegiones() async {
    var response = await rootBundle.loadString('assets/response.json');
    var results = (jsonDecode(response)['regions'] ?? []) as List;
    return results.map((x) => Regiones.fromJson(x)).toList();
    //return Regiones.fromJson(jsonDecode(response));
  }
  //Json['regions'] == null ? Null :

  @override
  Future<void> onInit() async {
    // TODO: implement onInit
    super.onInit();
    regionList.assignAll(await getRegiones());
  }
}

Json

{
  "name": "Chile",
  "regions": [
    {
      "name": "Arica y Parinacota",
      "romanNumber": "XV",
      "number": "15",
      "abbreviation": "AP",
      "communes": [
        { "name": "Arica", "identifier": "XV-1" },
        { "name": "Camarones", "identifier": "XV-2" },
        { "name": "General Lagos", "identifier": "XV-3" },
        { "name": "Putre", "identifier": "XV-4" }
      ]
    },
    {
...

Model

Regiones regionesFromJson(String str) => Regiones.fromJson(json.decode(str));

String regionesToJson(Regiones data) => json.encode(data.toJson());

class Regiones {
  Regiones({
    required this.name,
    required this.regions,
  });

  String name;
  List<Region> regions;

  factory Regiones.fromJson(Map<String, dynamic> json) => Regiones(
        name: json["name"],
        regions:
            List<Region>.from(json["regions"].map((x) => Region.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "name": name,
        "regions": List<dynamic>.from(regions.map((x) => x.toJson())),
      };
}

class Region {
  Region({
    required this.name,
    required this.romanNumber,
    required this.number,
    required this.abbreviation,
    required this.communes,
  });

  String? name;
  String? romanNumber;
  String? number;
  String? abbreviation;
  List<Commune> communes;

  factory Region.fromJson(Map<String, dynamic> json) => Region(
        name: json["name"],
        romanNumber: json["romanNumber"],
        number: json["number"],
        abbreviation: json["abbreviation"],
        communes: List<Commune>.from(
            json["communes"].map((x) => Commune.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "name": name,
        "romanNumber": romanNumber,
        "number": number,
        "abbreviation": abbreviation,
        "communes": List<dynamic>.from(communes.map((x) => x.toJson())),
      };
}

class Commune {
  Commune({
    required this.name,
    required this.identifier,
  });

  String name;
  String identifier;

  factory Commune.fromJson(Map<String, dynamic> json) => Commune(
        name: json["name"],
        identifier: json["identifier"] ?? '',
      );

  Map<String, dynamic> toJson() => {
        "name": name,
        "identifier": identifier,
      };
}

CodePudding user response:

You call ['regions'] in two place:

1:

var results = (jsonDecode(response)['regions'] ?? []) as List;

2: inside Regiones.fromJson so in your HomeController instead of this:

return results.map((x) => Regiones.fromJson(x)).toList();

try this:

return results.map((x) => Region.fromJson(x)).toList();

and then make your getRegiones return Future<List> like this:

Future<List<Regione>> getRegiones() async {
...
}
  • Related