Hi, I just started writing kotlin. I have this fun...
# getting-started
a
Hi, I just started writing kotlin. I have this function:
Copy code
private fun Toolbar() {
    TopAppBar(
        title = { Text(text = "About Device") }
    )
}
This is my import for Text
Copy code
import androidx.compose.material3.Text
The
Text
is underlined red and I have this error
Copy code
Overload resolution ambiguity. All these functions match.

public fun Text(text: String, modifier: Modifier = ..., color: Color = ..., fontSize: TextUnit = ..., fontStyle: FontStyle? = ..., fontWeight: FontWeight? = ..., fontFamily: FontFamily? = ..., letterSpacing: TextUnit = ..., textDecoration: TextDecoration? = ..., textAlign: TextAlign? = ..., lineHeight: TextUnit = ..., overflow: TextOverflow = ..., softWrap: Boolean = ..., maxLines: Int = ..., minLines: Int = ..., onTextLayout: ((TextLayoutResult) → Unit)? = ..., style: TextStyle = ...): Unit defined in androidx. compose. material3
public fun Text(text: String, modifier: Modifier = ..., color: Color = ..., fontSize: TextUnit = ..., fontStyle: FontStyle? = ..., fontWeight: FontWeight? = ..., fontFamily: FontFamily? = ..., letterSpacing: TextUnit = ..., textDecoration: TextDecoration? = ..., textAlign: TextAlign? = ..., lineHeight: TextUnit = ..., overflow: TextOverflow = ..., softWrap: Boolean = ..., maxLines: Int = ..., minLines: Int = ..., onTextLayout: (TextLayoutResult) → Unit = ..., style: TextStyle = ...): Unit defined in androidx. compose. material3
any help with it ? Thanks
r
You may be better off asking in #C0B8M7BUY, it looks like it's a matter of how to use the Android Text class. The error is telling you that there are two `fun Text`s which can take just a String ( your
Text(text = "About Device")
). The difference between them seems to be that one takes a parameter
onTextLayout
which has type
((TextLayoutResult) → Unit)?
(a function, but can be null), defaulted to something, and one takes a parameter
onTextLayout
which has type
(TextLayoutResult) → Unit
(a function, but cannot be null) which is also defaulted. You can probably resolve it by passing a value as
onTextLayout
, either
null
or
{_ -> }
, but it seems an odd API design for Android.
(I wonder if your compile classpath is a bit mucked up? It seems wrong that you've got both those functions on the compile classpath at the same time...)
e
there are only two Text overloads defined by material3, and they are not ambiguous (String/AnnotatedString parameter)
however, one of the conflicting overloads there appears to be from material (material design 2)
you should not import both, or at least you should alias one while importing, to avoid confusion like this
a
@ephemient I deleted the material3 implementation that was not in use and everything works fine. Thank you. @Rob Elliot thank you as well