티스토리 뷰

Before

- There was no null-handling in the method "firstWhere" which throws an error "Bad State" once it returns "null" value.

String? title = e.inspections
	.firstWhere((e) => e.name == inspection.name)
    .value; 
 
text = Text( title ?? '-');

 

After

- Add "orElse" caluse inside the method but note that value "null" is unavailable to set. In my case, the returned value is an object of "Inspection" that is why I set "null" as a value of a field "value" in a singular object. Value is also availble to be "" (empty string). Briefly,  your "null" value without encapsulating is UNavailable. 

String? title = e.inspections
	.firstWhere((e) => e.name == inspection.name,
    	orElse: () => Inspection(value: null))
    .value; 
 
text = Text( title ?? '-');

 

👨‍🏫 Recommendation

- Highly recommend to use "collection" package to handle null easily. It provides "firstWhereOrNull", "lastWhereOrNull", "singleWhereOrNull". 

import 'package:collection/collection.dart';

 final prevIns = prevCheckUp!.inspections.firstWhereOrNull(
            (e) => e.key == inspection.key,
          );