Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Build Stateless Widget from Dynamic Map

I am new into flutter, I would like to build dynamic Map<key,value> to a stateless widget. I have tried to use as many tutorials given but it can only works if it is a list. If it is map which contain such as

final Map toCheck = {‘bedroom’:’bed’,’bathroom’:’bathtub_outlined’};

It shows an error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

lib/template/general.dart:164:18: Error: A value of type ‘Row’ can’t
be returned from a function with return type ‘MapEntry<dynamic,
dynamic>’.

  • ‘Row’ is from ‘package:flutter/src/widgets/basic.dart’ (‘../flutter/packages/flutter/lib/src/widgets/basic.dart’).
  • ‘MapEntry’ is from ‘dart:core’.
    return Row(
    ^ lib/template/general.dart:170:12: Error: The method ‘toList’ isn’t defined for the class ‘Map<dynamic, dynamic>’.
  • ‘Map’ is from ‘dart:core’. Try correcting the name to the name of an existing method, or defining a method named ‘toList’.
    }).toList(),
    ^^^^^^

Here is the stateless widget function

class BundleSpecification extends StatelessWidget {
  final Map specification;
  BundleSpecification(this.specification);

  final Map toCheck = {'bedroom':'bed','bathroom':'bathtub_outlined'};
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(0, 15.0, 0, 15.0),
      child: Row(
        children: toCheck.map((type,value){
          return Row(
            children: [
              Icon(Icons.bed),
              Text(specification[value]),
            ],
          );
        }).toList(),
      )
    );
  }

I really appreciate any answers. Thank you.

>Solution :

Row takes an Iterable (for example a List) as argument for children. You provide it a map which obviously does not work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading