🤷‍♀️

unresolved reference: fillmaxsize

2024/02/02に公開

Androidでimportできない😇

unresolved reference: fillmaxsizeこんなエラーが出たことある?
Option + Enter押したら自動importじゃないのか???

こちらのサイトを参考に、import文を追加したら解決できた???
https://stackoverflow.com/questions/77311400/why-i-getting-a-error-unresolved-reference-fillmaxsize-with-modifier-modifi

これを追加する

import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

Boxで、TextFieldを中央寄せにできた🙌

Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            TextField(
                value = title,
                onValueChange = { title = it },
                label = { Text("title") }
            )

全体のコード:

package com.example.todofireapp

import TodoItem
import android.content.ContentValues.TAG
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.Alignment
import com.example.todofireapp.ui.theme.TodoFireAppTheme
import com.google.firebase.Firebase
import com.google.firebase.firestore.firestore
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TodoFireAppTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting()
                }
            }
        }
    }
}

@Composable
fun Greeting() {
    val db = Firebase.firestore
    var title by remember { mutableStateOf("") }
    val todos = remember { mutableStateOf(listOf<TodoItem>()) }

    LaunchedEffect(key1 = Unit) {
        db.collection("todo")
            .get()
            .addOnSuccessListener { result ->
                for (document in result) {
                    Log.d(TAG, "${document.id} => ${document.data}")
                    todos.value = todos.value + TodoItem(document.id, document.data["title"].toString())
                }
            }
            .addOnFailureListener { exception ->
                Log.w(TAG, "Error getting documents.", exception)
            }
    }

    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            TextField(
                value = title,
                onValueChange = { title = it },
                label = { Text("title") }
            )

            Spacer(modifier = Modifier.height(8.dp))

            Button(onClick = {
                val city = hashMapOf(
                    "title" to title
                )
                db.collection("todo")
                    .add(city)
                    .addOnSuccessListener { documentReference ->
                        Log.d(TAG, "DocumentSnapshot added with ID: ${documentReference.id}")
                        todos.value = todos.value + TodoItem(documentReference.id, title)
                        title = ""
                    }
                    .addOnFailureListener { e ->
                        Log.w(TAG, "Error adding document", e)
                    }
            }) {
                Text(text = "Add Todo")
            }

            Spacer(modifier = Modifier.height(16.dp))

            LazyColumn {
                items(todos.value) { todo ->
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween
                    ) {
                        Text(text = todo.title)
                        IconButton(onClick = {
                            db.collection("todo").document(todo.id)
                                .delete()
                                .addOnSuccessListener {
                                    Log.d(TAG, "DocumentSnapshot successfully deleted!")
                                    todos.value = todos.value.filter { it.id != todo.id }
                                }
                                .addOnFailureListener { e ->
                                    Log.w(TAG, "Error deleting document", e)
                                }
                        }) {
                            Icon(Icons.Filled.Delete, contentDescription = "Delete")
                        }
                    }
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    TodoFireAppTheme {
        Greeting()
    }
}

まとめ

Android Studioを新しくしたのでその影響かと思ったのですがどうやら違うようでした。import文を追加すれば解決できたと思われます🤔
SwiftUIだったら自動でimportしてくれて、Flutterだったらショートカットキーを押せばimportしてくれます。
Androidはよくわからない🙃

Discussion