【发布时间】:2021-04-18 17:08:32
【问题描述】:
我有
Error: Method 'group' cannot be called on 'RegExpMatch?' because it is potentially null.
- 'RegExpMatch' is from 'dart:core'.
Try calling using ?. instead.
final everything = match.group(0);
我的源代码在下面,只是一个简单的正则表达式。
我应该在哪里解决这个问题??
RegExp regExp = new RegExp(r'[0-9][0-9]');
RegExpMatch match = regExp.firstMatch(results.text);
final everything = match.group(0);
我是这样改源码的
RegExp regExp = new RegExp(r'C(S|N)-[0-9][0-9][0-9]-[0-9][0-9][0-9]');
if (regExp.hasMatch(results.text)){
RegExpMatch match = regExp.firstMatch(results.text);
final everything = match!.group(0);
_showMyDialog(everything);
}
仍然发生错误。
lib/camera_preview_scanner.dart:77:44: Error: A value of type 'RegExpMatch?' can't be assigned to a variable of type 'RegExpMatch' because 'RegExpMatch?' is nullable and 'RegExpMatch' isn't.
- 'RegExpMatch' is from 'dart:core'.
RegExpMatch match = regExp.firstMatch(results.text);
^
lib/camera_preview_scanner.dart:79:36: Warning: Operand of null-aware operation '!' has type 'RegExpMatch' which excludes null.
- 'RegExpMatch' is from 'dart:core'.
final everything = match!.group(0);
解决方案
使用RegExp! 代替RegExp
RegExp! regExp = new RegExp(r'C(S|N)-[0-9][0-9][0-9]-[0-9][0-9][0-9]');
if (regExp.hasMatch(results.text)){
RegExpMatch match = regExp.firstMatch(results.text);
final everything = match!.group(0);
_showMyDialog(everything);
}
【问题讨论】: