0

I am trying to figure out about null where I need to return a default value it contains null. Here is the code (Please check the comment in the code)

class BundlingSpecification extends StatelessWidget {
  final DataAd object;
  BundlingSpecification({required this.object});

  List toCheck = ['lt','lb','bedroom','bathroom'];
  Map specification = {};
  Map<String,Widget> icon = {
    'bedroom':Icon(Icons.bed), //Icon to Display
    'bathroom':Icon(Icons.bathtub_outlined), //Icon to Display
  };

  @override
  Widget build(BuildContext context) {
    if(object.data!=null) {
      final data = object.data!['additional'];
      for (var u in toCheck) {
        if(data[u]!=null && data[u]!=''){
          final Map result = {
            'type':u,
            'show':data[u],
          };
          specification[u] = result;
        }
      }
    }
    return Row(
      children: specification.map((key, value) => MapEntry(key, Row(
        children: [
          if(icon[key]!=null)...[ /// WHEN IS NOT NULL (Displaying Icon)
            icon[key]!,
          ] else ...[ /// WHEN IS NULL RETURN TEXT ONLY
            Text(key),
          ],
          Text(value['show']),
        ],
      ))).values.toList(),
    );
  }
}

The problem, I received this following error

lib/class/adClass.dart:452:18: Error: A value of type 'List' can't be assigned to a variable of type 'Widget'.

  • 'List' is from 'dart:core'.
  • 'Text' is from 'package:flutter/src/widgets/text.dart' ('../flutter/packages/flutter/lib/src/widgets/text.dart').
  • 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../flutter/packages/flutter/lib/src/widgets/framework.dart'). ] else [ ^

1 Answer 1

1

You aren't destructuring the array for the else part.

else ...[ 
  Text(key),
]

This should fix your code ideally.

Sign up to request clarification or add additional context in comments.

1 Comment

Just click the check icon to mark this question as solved 😄

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.