Home > OS >  A value of type 'List<Map<String, dynamic>>' can't be assigned to a varia
A value of type 'List<Map<String, dynamic>>' can't be assigned to a varia

Time:05-29

I'm tryng to get a list of boxes to show in a dropdown. But I am can't convert to list map to my object list.

Here's the error message:

A value of type 'List<Map<String, dynamic>>' can't be assigned to a variable of type 'List'. Try changing the type of the variable, or casting the right-hand type to 'List'.

Here's the code:

import 'package:flutter/material.dart';
import 'package:trfcompactapp/constants.dart';
import '../model/sql_helper.dart';
import '../screens/drawer.dart';
import '../controller/localizationstrf.dart';

class ProjectBoxes {
  ProjectBoxes(this.boxId, this.boxName, this.lenght, this.width, this.weight,
      this.useLabel, this.labelOrientation, this.createdAt);

  int boxId;
  String boxName;
  int lenght;
  int width;
  int weight;
  int useLabel;
  int labelOrientation;
  DateTime createdAt;
}

class Project extends StatefulWidget {
  const Project({super.key});

  @override
  State<Project> createState() => _ProjectState();
}

class _ProjectState extends State<Project> {
  late ProjectBoxes selectedProjectBox;

  @override
  void initState() {
    super.initState();
    _refreshJournals();

    
  }

  List<Map<String, dynamic>> _journals = [];
  List<Map<String, dynamic>> _journalBoxes = [];

  bool _isLoading = true;
  // This function is used to fetch all data from the database
  void _refreshJournals() async {
    final data = await SQLHelper.getProjects();
    final dataBoxes = await SQLHelper.getBoxes();
    setState(() {
      _journals = data;
      _isLoading = false;
      _journalBoxes = dataBoxes;
      


      boxes = _journalBoxes;  // in this line the error happens.

      
    });
  }

  late List<ProjectBoxes> boxes = <ProjectBoxes>[];

  final TextEditingController _projectNameController = TextEditingController();
  final TextEditingController _fkProjectBoxController = TextEditingController();
  final TextEditingController _fkProjectPalletController =
      TextEditingController();
  final TextEditingController _fkProjectRobotController =
      TextEditingController();

  void _showForm(int? id) async {
    if (id != null) {
      final existingJournal =
          _journals.firstWhere((element) => element['projectId'] == id);
      final existingJournalBox = _journalBoxes.firstWhere(
          (element) => element['boxId'] == existingJournal['FK_project_box']);
      _projectNameController.text = existingJournal['projectName'];
      _fkProjectBoxController.text =
          existingJournalBox['FK_project_box'].toString();
      _fkProjectPalletController.text =
          existingJournal['FK_project_pallet'].toString();
      _fkProjectRobotController.text =
          existingJournal['FK_project_robot'].toString();

      selectedProjectBox = existingJournalBox[0];
      boxes = existingJournalBox[0];
    }
   

CodePudding user response:

It trys to assign List<Map> to a List. You'll have to write a converter to convert the data inside the Map to new ProjectBoxes objects.

Instead of boxes = _journalBoxes; use the following loop:

for (Map<String, dynamic> item in _journalBoxes) {
  boxes.add(ProjectBoxes(
    field1: item["firstField"] ?? "default value",
    field2: item["thirdField"],
    field3: item["secondField"] ?? false,
  ));
}

I don't know the structure of ProjectBoxes so I added some example values:

class ProjectBoxes {
  // example fields
  String field1;
  int? field2;
  bool field3;
  
  // constructor
  ProjectBoxes({
    required this.field1, 
    this.field2, 
    required this.field3,
  });
}
  • Related