最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Flutter typing of Map keys - Stack Overflow

programmeradmin3浏览0评论

I have the following Flutter code:

class Facility{
  Facility({
    required this.name,
    required this.rooms
  });

  final String name;
  final List<Map<String, dynamic>> rooms;

  Map<String, dynamic> toFirestore() {
    return {
      'name': name,
      'rooms': rooms,
    };
  }

  factory Facility.fromDummy(Map<String, dynamic> data) {
    return facilityModelFactory(data);
  }

  factory Facility.fromFirestore(
    DocumentSnapshot<Map<String, dynamic>> snapshot,
    SnapshotOptions? options,
  ) {

    // get data from snapshot
    final data = snapshot.data()!;

    // make client instance
    Facility facility = facilityModelFactory(data);

    return facility;
  }
}

Facility facilityModelFactory(Map<String, dynamic> data) {
  return Facility(
    name: data['name'],
    rooms: data['rooms']   << -------------- ERROR
  );
}

The problem is the line flagged with "Error" is rising the following error:

E/flutter ( 4179): [ERROR:flutter/runtime/dart_vm_initializer(40)] Unhandled Exception: type 'List' is not a subtype of type 'List<Map<String, dynamic>>'

How can I declare a Map with a key that is Dynamic type, like rooms in the example, and assign a value as I'm attempting in method facilityModelFactory()?

I have the following Flutter code:

class Facility{
  Facility({
    required this.name,
    required this.rooms
  });

  final String name;
  final List<Map<String, dynamic>> rooms;

  Map<String, dynamic> toFirestore() {
    return {
      'name': name,
      'rooms': rooms,
    };
  }

  factory Facility.fromDummy(Map<String, dynamic> data) {
    return facilityModelFactory(data);
  }

  factory Facility.fromFirestore(
    DocumentSnapshot<Map<String, dynamic>> snapshot,
    SnapshotOptions? options,
  ) {

    // get data from snapshot
    final data = snapshot.data()!;

    // make client instance
    Facility facility = facilityModelFactory(data);

    return facility;
  }
}

Facility facilityModelFactory(Map<String, dynamic> data) {
  return Facility(
    name: data['name'],
    rooms: data['rooms']   << -------------- ERROR
  );
}

The problem is the line flagged with "Error" is rising the following error:

E/flutter ( 4179): [ERROR:flutter/runtime/dart_vm_initializer(40)] Unhandled Exception: type 'List' is not a subtype of type 'List<Map<String, dynamic>>'

How can I declare a Map with a key that is Dynamic type, like rooms in the example, and assign a value as I'm attempting in method facilityModelFactory()?

Share Improve this question edited Feb 15 at 20:50 HuLu ViCa asked Feb 15 at 17:34 HuLu ViCaHuLu ViCa 5,45212 gold badges50 silver badges113 bronze badges 2
  • List<Map<String, dynamic>> rooms should really be List<Room>, and create a data class to hold Room. – Randal Schwartz Commented Feb 15 at 19:30
  • Can you create a small example code that more easily explains the situation? For example something that it could be easily run on DartPad? – pratikpc Commented Feb 15 at 19:40
Add a comment  | 

1 Answer 1

Reset to default 0

Try this:

Facility facilityModelFactory(Map<String, dynamic> data) {  
   List<Map<String, dynamic>> rooms = List.from(data['rooms']);  
   return Facility(name: data['name'], rooms: rooms);
}
发布评论

评论列表(0)

  1. 暂无评论