Home > database >  I'm trying to create a List inside a Object inside a List in flutter, help me
I'm trying to create a List inside a Object inside a List in flutter, help me

Time:09-06

I just need to put a Object with 2 keys inside a Array/List inside a Object (Classe()) inside a List.

class Classe {
  final int id;
  final String title, icon;
  final List<Object> content;
  final Color color;

  Classe({
    required this.id,
    required this.title,
    required this.content,
    required this.icon,
    required this.color,
  });
}
List<Classe> classes_ = [
  Classe(
    id: 0,
    title: "Title 1",
    content: [
      {"type": "text", "src": "Description text 1"},
      {"type": "file", "src": "assets/img/logo.png"},
    ],
    icon: "assets/img/settings.png",
    color: const Color(0xFF000000),
  ),
  Classe(
    id: 1,
    title: "Title 2",
    content: [
      {"type": "text", "src": "Description text 2"},
      {"type": "file", "src": "assets/img/img2.png"},
    ],
    icon: "assets/img/img2.png",
    color: const Color(0xFFFFFFFF),
  ),
];

The error: lib/pages/classe_view.dart:45:45: Error: The argument type 'Object' can't be assigned to the parameter type 'String'.

  • 'Object' is from 'dart:core'. image: AssetImage(classe.content[0]), ^ image: AssetImage(classe.content[0].src), classe is just "classes_[0]".

CodePudding user response:

content should be of type List<Map<String, String>> rather than List<Object>.

  final int id;
  final String title, icon;
  final List<Map<String, String>> content;
  final Color color;

  Classe({
    required this.id,
    required this.title,
    required this.content,
    required this.icon,
    required this.color,
  });
}
  • Related