How to Build Stateless Widget from Dynamic Map

Issue

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

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.

Answered By – JustLearnedIt

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.