Home > Software design >  If Value is Null Then Return Values [Error]
If Value is Null Then Return Values [Error]

Time:04-04

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 [ ^

CodePudding user response:

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

else ...[ 
  Text(key),
]

This should fix your code ideally.

  • Related