Home > Net >  flutter null safety Regex group error : A value string? can't be assigned to type String
flutter null safety Regex group error : A value string? can't be assigned to type String

Time:06-12

I try to make flutter nullsafety migration, but I have an error with match.group(1); The error is : A value string? can't be assigned to type String

Here is my code

late String date_modify ;

RegExp regExp = new RegExp(          
          r"(.{0,10}(?!\w))",
        );

    var date=DateTime.now();
    DateTime daysfromnow = date.add(new Duration(days: 0));

    var match = regExp.firstMatch("$daysfromnow");
    date_modify = match.group(1); 

var _lastConso_sec = date_modify!=null ? DateTime.parse(date_modify) : DateTime.now();

If I convert late String date_modify; => late String? date_modify ;

It's ok with

date_modify = match!.group(1); 

but I have now problem with DateTime.parse(date_modify)

the argument type String ? can't be assigned to the parameter type String

CodePudding user response:

Change the data type of date_modify from String to String?

String? dateModify;
date_modify = match!.group(1); 
DateTime.parse(dateModify!);
  • Related