1

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.

2 Answers 2

0

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

3
  • so, how about doing foreach inside children of Row? What approach do you suggest anyway? Mar 20, 2022 at 18:35
  • see @esentis answer
    – Banana
    Mar 20, 2022 at 18:39
  • Hi, do you know how to set Icon(Icons.bed) to Icon(Icons.[callVariable])? Mar 20, 2022 at 18:49
0

You can try mapping the Map in your Row widget this way:

Row(
 children: [
  ...toCheck.entries.map(
   (x) => Row(
     children: [
       Icon(Icons.bed),
       Text(specification[x.value]),
    ],
   ),
  )
 ],
)
3
  • Hi, I forgot to set Icon(Icons.bed) into dynamic. How to approach Icon(Icons.[x.key]) Mar 20, 2022 at 18:44
  • I don't think you can. You'll have to manually handle your cases in a function e.g if (x.key=='bed') return Icons.bed
    – esentis
    Mar 20, 2022 at 18:51
  • 1
    I see, I thought it could be called from dynamic var. Thank you Mar 20, 2022 at 19:03

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.