'List<Map<String, dynamic>>' 类型的值不能分配给 'List<ProjectBoxes>' 类型的变量

A value of type 'List<Map<String, dynamic>>' can't be assigned to a variable of type 'List<ProjectBoxes>'

我正在尝试获取要在下拉列表中显示的框列表。但是我无法将列表映射转换为我的对象列表。

错误信息如下:

无法将 'List<Map<String, dynamic>>' 类型的值分配给 'List' 类型的变量。 尝试更改变量的类型,或将右侧的类型转换为 'List'.

代码如下:

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];
    }
   

它试图将 List<Map> 分配给一个列表。您必须编写一个转换器来将 Map 中的数据转换为新的 ProjectBoxes 对象。

而不是 boxes = _journalBoxes; 使用以下循环:

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

我不知道 ProjectBoxes 的结构,所以我添加了一些示例值:

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