Home > Software engineering >  How to Build Stateless Widget from Dynamic Map
How to Build Stateless Widget from Dynamic Map

Time:03-21

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.

CodePudding user response:

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

CodePudding user response:

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]),
    ],
   ),
  )
 ],
)
  • Related