flutterandroid-activityqr-codedart-null-safetyflutter-upgrade

How can I edit the MainActivity.kt file in my flutter project and how is it even created?


I upgraded a flutter project from 2.10 to 3.3 (same error occured on 3.0).

This is the error im getting:

e: path/MainActivity.kt: (82, 34): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List? e: path/MainActivity.kt: (83, 37): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List? e: path/MainActivity.kt: (84, 37): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List? e: path/MainActivity.kt: (85, 38): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List?

FAILURE: Build failed with an exception.

Compilation error. See log for more details

BUILD FAILED in 11s

This is the part in the MainActivity.kt file:

MethodChannel(flutterEngine.dartExecutor.binaryMessenger, LUCA_CHANNEL).setMethodCallHandler{
            call, result ->
            when (call.method) {
                "isLucaQRCode" -> {
                    val qrCode = call.argument<String>("qrCode")
                    if(qrCode != null) {
                        result.success(lucaEvaluator.isLucaQRCode(qrCode))
                    }
                }
                "processLucaQRCode" -> {
                    val qrCode = call.arguments<List<String>>()[0];
                    val scannerId = call.arguments<List<String>>()[1];
                    val publicKey = call.arguments<List<String>>()[2];
                    val locationId = call.arguments<List<String>>()[3];
                    if(qrCode != null && scannerId != null && publicKey != null && locationId != null) {
                        result.success(lucaEvaluator.processLucaQRCodeToStringOutput(qrCode,scannerId,publicKey,locationId))
                    }
                }
            }
        }
    }

This is the function in my normal code:

  Future<LucaEvaluationResult?> processLucaQRCode(String? qrCode, String? eventId) async {
    final lucaConfiguration = _getLucaConfiguration(eventId);
    if(lucaConfiguration == null || qrCode == null) {
      return null;
    }
    try {
      final res = await methodChannel.invokeMethod('processLucaQRCode', [
        qrCode,
        lucaConfiguration.lucaScannerId,
        lucaConfiguration.lucaLocationPublicKey,
        lucaConfiguration.lucaLocationId,
      ]);
      if(res != null) {
        return LucaEvaluationResult.fromJson(json.decode(res));
      }
    } on Exception catch (e) {
      //log(e.toString());
    }
    return null;
  }

I do understand the problem with the nullability but how do I fix it. I don't know in which file I have to change something. How can I change the MainActivity.kt file. Is it generated?

Thanks for the help.


Solution

  • call.arguments<List<String>>() can be null so maybe do like this:

                "processLucaQRCode" -> {
                    val list = call.arguments<List<String>>()
                    if (list != null) {
                        val qrCode = list[0]
                        val scannerId = list[1]
                        val publicKey = list[2]
                        val locationId = list[3]
                        if(qrCode != null && scannerId != null && publicKey != null && locationId != null) {    
                            result.success(lucaEvaluator.processLucaQRCodeToStringOutput(qrCode,scannerId,publicKey,locationId))
                        }
                    }
                }