I get the following error:
e: C:\Users\Eric.Almendras\Desktop\project-flutter\scann\android\app\src\main\kotlin\com\darryncampbell\datawedgeflutter\MainActivity.kt: (79, 44): Type mismatch: inferred type is String? but String was expected
e: C:\Users\Eric.Almendras\Desktop\project-flutter\scann\android\app\src\main\kotlin\com\darryncampbell\datawedgeflutter\MainActivity.kt: (79, 54): Type mismatch: inferred type is String? but String was expected
I can't solve it.. my code is the following:
MainActivity.kt
package com.darryncampbell.datawedgeflutter
import android.content.*
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.EventChannel.EventSink
import io.flutter.plugin.common.EventChannel.StreamHandler
import io.flutter.plugin.common.MethodChannel
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.*
import io.flutter.plugins.GeneratedPluginRegistrant;
class MainActivity: FlutterActivity() {
private val COMMAND_CHANNEL = "com.darryncampbell.datawedgeflutter/command"
private val SCAN_CHANNEL = "com.darryncampbell.datawedgeflutter/scan"
private val PROFILE_INTENT_ACTION = "com.darryncampbell.datawedgeflutter.SCAN"
private val PROFILE_INTENT_BROADCAST = "2"
private val dwInterface = DWInterface()
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
EventChannel(flutterEngine.dartExecutor, SCAN_CHANNEL).setStreamHandler(
object : StreamHandler {
private var dataWedgeBroadcastReceiver: BroadcastReceiver? = null
override fun onListen(arguments: Any?, events: EventSink?) {
dataWedgeBroadcastReceiver = createDataWedgeBroadcastReceiver(events)
val intentFilter = IntentFilter()
intentFilter.addAction(PROFILE_INTENT_ACTION)
intentFilter.addAction(DWInterface.DATAWEDGE_RETURN_ACTION)
intentFilter.addCategory(DWInterface.DATAWEDGE_RETURN_CATEGORY)
registerReceiver(
dataWedgeBroadcastReceiver, intentFilter)
}
override fun onCancel(arguments: Any?) {
unregisterReceiver(dataWedgeBroadcastReceiver)
dataWedgeBroadcastReceiver = null
}
}
)
MethodChannel(flutterEngine.dartExecutor, COMMAND_CHANNEL).setMethodCallHandler { call,
result ->
if (call.method == "sendDataWedgeCommandStringParameter")
{
val arguments = JSONObject(call.arguments.toString())
val command: String = arguments.get("command") as String
val parameter: String = arguments.get("parameter") as String
dwInterface.sendCommandString(applicationContext, command, parameter)
// result.success(0); // DataWedge does not return responses
}
else if (call.method == "createDataWedgeProfile")
{
createDataWedgeProfile(call.arguments.toString())
}
else {
result.notImplemented()
}
}
}
private fun createDataWedgeBroadcastReceiver(events: EventSink?): BroadcastReceiver? {
return object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action.equals(PROFILE_INTENT_ACTION))
{
// A barcode has been scanned
var scanData =
intent.getStringExtra(DWInterface.DATAWEDGE_SCAN_EXTRA_DATA_STRING)
var symbology =
intent.getStringExtra(DWInterface.DATAWEDGE_SCAN_EXTRA_LABEL_TYPE)
var date = Calendar.getInstance().getTime()
var df = SimpleDateFormat("dd/MM/yyyy HH:mm:ss")
var dateTimeString = df.format(date)
var currentScan = Scan(scanData, symbology, dateTimeString);
events?.success(currentScan.toJson())
}
// Could handle return values from DW here such as RETURN_GET_ACTIVE_PROFILE
// or RETURN_ENUMERATE_SCANNERS
}
}
}
private fun createDataWedgeProfile(profileName: String) {
// Create and configure the DataWedge profile associated with this application
// For readability's sake, I have not defined each of the keys in the DWInterface file
dwInterface.sendCommandString(this, DWInterface.DATAWEDGE_SEND_CREATE_PROFILE,
profileName)
val profileConfig = Bundle()
profileConfig.putString("PROFILE_NAME", profileName)
profileConfig.putString("PROFILE_ENABLED", "true") // These are all strings
profileConfig.putString("CONFIG_MODE", "UPDATE")
val barcodeConfig = Bundle()
barcodeConfig.putString("PLUGIN_NAME", "BARCODE")
barcodeConfig.putString("RESET_CONFIG", "true") // This is the default but never hurts to
specify
val barcodeProps = Bundle()
barcodeConfig.putBundle("PARAM_LIST", barcodeProps)
profileConfig.putBundle("PLUGIN_CONFIG", barcodeConfig)
val appConfig = Bundle()
appConfig.putString("PACKAGE_NAME", packageName) // Associate the profile with this
app
appConfig.putStringArray("ACTIVITY_LIST", arrayOf("*"))
profileConfig.putParcelableArray("APP_LIST", arrayOf(appConfig))
dwInterface.sendCommandBundle(this, DWInterface.DATAWEDGE_SEND_SET_CONFIG, profileConfig)
profileConfig.remove("PLUGIN_CONFIG")
val intentConfig = Bundle()
intentConfig.putString("PLUGIN_NAME", "INTENT")
intentConfig.putString("RESET_CONFIG", "true")
val intentProps = Bundle()
intentProps.putString("intent_output_enabled", "true")
intentProps.putString("intent_action", PROFILE_INTENT_ACTION)
intentProps.putString("intent_delivery", PROFILE_INTENT_BROADCAST) // "2"
intentConfig.putBundle("PARAM_LIST", intentProps)
profileConfig.putBundle("PLUGIN_CONFIG", intentConfig)
dwInterface.sendCommandBundle(this, DWInterface.DATAWEDGE_SEND_SET_CONFIG, profileConfig)
}
}
Scan.kt
package com.darryncampbell.datawedgeflutter
import org.json.JSONObject;
class Scan(val data: String, val symbology: String, val dateTime: String)
{
fun toJson(): String{
return JSONObject(mapOf(
"scanData" to this.data,
"symbology" to this.symbology,
"dateTime" to this.dateTime
)).toString();
}
}
can someone see my mistake?
CodePudding user response:
kotlin is designed in a null safe matter. String?
basically means, a String
that can possibly be null. So kotlin disallows assigning String?
where it expects String
.
Your error says exactly where it happens.
MainActivity.kt: (79, 44)
in your log means it's on line 79 of your code.
I'm guessing it's the line
var currentScan = Scan(scanData, symbology, dateTimeString);
scanData
and symbology
are of type String?
but Scan
requires String
there. One easy way to convert it is to write it as
var currentScan = Scan(scanData!!, symbology!!, dateTimeString);
With !!
you basically say, "I'm sure this is not null". The code will crash though if it does happen to be null
CodePudding user response:
This is related to null safety in Kotlin. Your are inferring string as nullable , but where it is being used, it is expecting it as non nullable string. You can check for null before using it or have a mechanism to check null values and handle it.
Check this link for more detailed description for nullable variables