{"id":10261,"date":"2023-06-23T01:43:43","date_gmt":"2023-06-23T08:43:43","guid":{"rendered":"https:\/\/www.couchbase.com\/developers\/tutorials\/dart-flutter-learning-key-value\/"},"modified":"2023-06-23T01:43:43","modified_gmt":"2023-06-23T08:43:43","slug":"dart-flutter-learning-key-value","status":"publish","type":"tutorials","link":"https:\/\/www.couchbase.com\/pt\/developers\/tutorials\/dart-flutter-learning-key-value\/","title":{"rendered":"Learn Couchbase Lite Key Value Engine with Dart and Flutter"},"content":{"rendered":"\n<h3 id=\"h-introduction\" class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this part of our learning path, you will walk through the &#8220;Audit Inventory&#8221; demo application of using Couchbase Lite in <em>standalone mode<\/em>, which means the database does not sync information from other resources using replication.  While the demo app has a lot of functionality, this step will walk you through:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Installing Couchbase Lite on Flutter<\/li>\n\n\n<li>Database operations for creating, opening, closing, and deleting a database<\/li>\n\n\n<li>Document create, read, update, and delete (CRUD) operations via key-value pair<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In this step of the learning path, you will be working with the code that allows users to log in and make changes to their user profile information.  User profile information is persisted as a <code>Document<\/code> in the local Couchbase Lite <code>Database<\/code>. When the user logs out and logs back in again, the profile information is loaded from the <code>Database<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"user_profile.gif\" alt=\"User Profile App Demo,400\"><\/p>\n\n\n\n<h3 id=\"h-installation\" class=\"wp-block-heading\">Installation<\/h3>\n\n\n\n<h4 id=\"h-fetching-app-source-code\" class=\"wp-block-heading\">Fetching App Source Code<\/h4>\n\n\n\n<h5 id=\"h-clone-source-code\" class=\"wp-block-heading\">Clone Source Code<\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clone the <code>Learn Couchbase Lite with Kotlin and Jetpack Compose<\/code> repository from GitHub.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>git clone https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path.git\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-installing-couchbase-lite-framework\" class=\"wp-block-heading\">Installing Couchbase Lite Framework<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Demo app already contains the appropriate additions for downloading and utilizing the Kotlin Android Couchbase Lite dependency module.  However, in the future, to include Couchbase Lite support within an Andorid app add the following within <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/build.gradle#L21\">src\/build.gradle<\/a><\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>allprojects {\n    repositories {\n        ...\n\n        maven {\n            url &quot;https:\/\/mobile.maven.couchbase.com\/maven2\/dev\/&quot;\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then add the following to the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/build.gradle#L130\">app\/build.gradle<\/a> file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>dependencies {\n    ...\n\n    implementation &quot;com.couchbase.lite:couchbase-lite-android-ktx:3.0.0&quot;\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-try-it-out\" class=\"wp-block-heading\">Try it out<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open src\/build.gradle using Android Studio.<\/li>\n\n\n<li>Build and run the project.<\/li>\n\n\n<li>Verify that you see the login screen.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"user_profile_login.png\" alt=\"User Profile Login Screen Image\" title=\"#width=250px\"><\/p>\n\n\n\n<h3 id=\"h-data-model\" class=\"wp-block-heading\">Data Model<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Couchbase Lite is a JSON Document Store. A <code>Document<\/code> is a logical collection of named fields and values. The values are any valid JSON types. In addition to the standard JSON types, Couchbase Lite supports <code>Date<\/code> and <code>Blob<\/code> data types. While it is not required or enforced, it is a recommended practice to include a <em>&#8220;type&#8221;<\/em> property that can serve as a namespace for related documents.<\/p>\n\n\n\n<h4 id=\"h-the-user-profile-document\" class=\"wp-block-heading\">The User Profile Document<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The sample app deals with a single <code>Document<\/code> with a <em>&#8220;documentType&#8221;<\/em> property of <em>&#8220;user&#8221;<\/em>.  The document ID is of the form <strong>&#8220;user::&lt;email&gt;&#8221;<\/strong>.\nAn example of a document would be:<\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{\n  &quot;surname&quot;:&quot;Doe&quot;,\n  &quot;givenName&quot;:&quot;Jane&quot;,\n  &quot;jobTitle&quot;:&quot;Developer&quot;,\n  &quot;team&quot;:&quot;team1&quot;,\n  &quot;email&quot;:&quot;demo@example.com&quot;\n  &quot;documentType&quot;:&quot;user&quot;,\n  &quot;imageData&quot;:\n  {\n   &quot;length&quot;:217527,\n   &quot;content_type&quot;:&quot;image\/jpeg&quot;,\n   &quot;digest&quot;:&quot;sha1-+8y7vkRd2J95kVe\/yG2WhuEFa4o=&quot;,\n   &quot;@type&quot;:&quot;blob&quot;\n  },\n}\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Special <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.couchbase.com\/couchbase-lite\/3.0\/android\/blob.html\"><strong>Blob<\/strong><\/a> data type that is associated with the profile image.<\/p>\n\n\n\n<h5 id=\"h-team-membership\" class=\"wp-block-heading\">Team Membership<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">The team property designates which team a user is a member of.  The team property is a security boundary used as part of the database name, which means users in the same team will use the same database file if a user shares the same physical device or emulator.  To simplify things in this learning path, the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/src\/main\/java\/com\/couchbase\/learningpath\/services\/MockAuthenticationService.kt#L33\"><strong>MockAuthenticationService<\/strong><\/a> class defines users and which teams they belong to.  Security rules, team membership, and authentication would normally be handled in an OAuth provider or a custom system in an actual mobile application.<\/p>\n\n\n\n<h4 id=\"h-userprofile\" class=\"wp-block-heading\">UserProfile<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Open the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/src\/main\/java\/com\/couchbase\/learningpath\/ui\/profile\/UserProfileViewModel.kt#L101\"><em>UserProfileViewModel.kt<\/em><\/a> ViewModel in the com.couchbase.learningpath.ui.profile  directory.  For the purpose of this tutorial the <em>&#8220;user&#8221;<\/em> <code>Document<\/code> is first stored within an <code>Any<\/code> of type <code>HashMap&lt;String, Any&gt;<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>val profile = HashMap&lt;String, Any&gt;()\nprofile[&quot;givenName&quot;] = givenName.value as Any\nprofile[&quot;surname&quot;] = surname.value as Any\nprofile[&quot;jobTitle&quot;] = jobTitle.value as Any\nprofile[&quot;email&quot;] = emailAddress.value as Any\nprofile[&quot;team&quot;] = team.value as Any\nprofile[&quot;documentType&quot;] = &quot;user&quot; as Any\nprofilePic.value?.let {\n  val outputStream = ByteArrayOutputStream()\n  it.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)\n  profile[&quot;imageData&quot;] =\n      Blob(&quot;image\/jpeg&quot;, outputStream.toByteArray()) as Any\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>HashMap&lt;String, Any&gt;<\/code> object functions are used as a data storage mechanism between the app&#8217;s UI and the backing functionality of the Couchbase Lite <code>Document<\/code> object.<\/p>\n\n\n\n<h3 id=\"h-basic-database-operations\" class=\"wp-block-heading\">Basic Database Operations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we will do a code walkthrough of the basic Database operations.<\/p>\n\n\n\n<h4 id=\"h-initialize-couchbase-lite\" class=\"wp-block-heading\">Initialize Couchbase Lite<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Before you can start using Couchbase Lite on Android, you would have to initialize it. Couchbase Lite needs to be initialized with the appropriate Android Application Context.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/src\/main\/java\/com\/couchbase\/learningpath\/data\/DatabaseManager.kt#L36\"><strong>DatabaseManager.kt<\/strong><\/a> file and locate the <code>init<\/code> constructor.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>class InventoryDatabase private constructor (private val context: Context) {\n    ...\n    init {\n        \/\/setup couchbase lite\n        CouchbaseLite.init(context)\n        ...\n    }\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-create-and-open-a-database\" class=\"wp-block-heading\">Create and Open a Database<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When a user logs in, we create an empty Couchbase Lite database if one does not exist.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/src\/main\/java\/com\/couchbase\/learningpath\/data\/DatabaseManager.kt#L69\"><strong>DatabaseManager.kt<\/strong><\/a> file and locate the <code>initializeDatabases<\/code> function.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>fun initializeDatabases(currentUser: User) \n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">NOTE:  You will notice that this code has initialization for two different databases, the inventory database that we will be usign in this step, and the warehouse database.  For this step you can ignore the code that initializes the warehouse database as we will cover it in a later step.<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We create an instance of the <code>DatabaseConfiguration<\/code> within <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/src\/main\/java\/com\/couchbase\/learningpath\/data\/DatabaseManager.kt#L73\"><strong>DatabaseManager.kt<\/strong><\/a>. Each team has their own instance of the Database that is located in a folder corresponding to the team name.  Please note that the default path is platform-specific.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>val dbConfig = DatabaseConfigurationFactory.create(context.filesDir.toString())\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Then we create a local Couchbase Lite database named <strong>&#8220;<em>teamname<\/em>_userprofiles&#8221;<\/strong>. If a database already exists, the existing version is returned.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>\/\/ create or open a database to share between team members to store\n\/\/ projects, assets, and user profiles\n\/\/ calculate database name based on current logged in users team name\nval teamName = (currentUser.team.filterNot { it.isWhitespace() }).lowercase()\ncurrentInventoryDatabaseName = teamName.plus(&quot;_&quot;).plus(defaultInventoryDatabaseName)\ninventoryDatabase = Database(currentInventoryDatabaseName, dbConfig)\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-close-a-database\" class=\"wp-block-heading\">Close a Database<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When a user logs out or the app goes to the background, we close the Couchbase Lite database.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/src\/main\/java\/com\/couchbase\/learningpath\/data\/DatabaseManager.kt#L60\"><strong>DatabaseManger.kt<\/strong><\/a> file and locate the <code>closeDatabases()<\/code> function.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>fun closeDatabases() {\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Closing the database is pretty straightforward<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>inventoryDatabase?.close()\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-deleting-a-database\" class=\"wp-block-heading\">Deleting a Database<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Deletion of a database is pretty straightforward and this is how you would do it.<\/p>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>fun deleteDatabases() {\n  try {\n    closeDatabases()\n    Database.delete(currentInventoryDatabaseName, context.filesDir)\n    Database.delete(warehouseDatabaseName, context.filesDir)\n  } catch (e: Exception) {\n    android.util.Log.e(e.message, e.stackTraceToString())\n  }\n}\n\nfun closeDatabases() {\n  try {\n    inventoryDatabase?.close()\n    locationDatabase?.close()\n  } catch (e: java.lang.Exception) {\n    android.util.Log.e(e.message, e.stackTraceToString())\n  }\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-try-it-out\" class=\"wp-block-heading\">Try it out<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The app can be tested using a simulator\/emulator or device.<\/li>\n\n\n<li>Log in to the app with any username and password. Let&#8217;s use the values <em>&#8220;<strong><a href=\"mailto:demo@example.com\">demo@example.com<\/a><\/strong>&#8220;<\/em> and <em>&#8220;<strong>P@ssw0rd12<\/strong>&#8220;<\/em> for username and password fields respectively. If this is the first time that the user is signing in, a new Couchbase Lite database will be created. If not, the existing database will be opened.<\/li>\n\n<\/ul>\n\n\n\n<h3 id=\"h-document-operations\" class=\"wp-block-heading\">Document Operations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once an instance of the Couchbase Lite database is created\/opened for the specific user, we can perform basic <code>Document<\/code> functions on the database. In this section, we will walk through the code that describes basic <code>Document<\/code> operations<\/p>\n\n\n\n<h4 id=\"h-reading-a-document\" class=\"wp-block-heading\">Reading a Document<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Once the user logs in, the user is taken to the &#8220;Projects&#8221; screen.  From this screen the user would tap on the Drawer menu icon (sometimes referred to the Hamburger icon) in the upper left hand corner of the screen and tap on the Update User Profile text button.  A request is made to load <a href=\"#the-user-profile-document\">The &#8220;User Profile&#8221; Document<\/a> for the user. When the user logs in the very first time, there would be no <em>user profile<\/em> document for the user.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/src\/main\/java\/com\/couchbase\/learningpath\/data\/userprofile\/UserProfileRepository.kt#L97\"><strong>UserProfileRepository.kt<\/strong><\/a> file and locate the <code>getCurrentUserDocumentId<\/code> function. This document Id is constructed by prefixing the term &#8220;user::&#8221; to the email Id of the user.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>private fun getCurrentUserDocumentId(currentUser: String): String {\n  return &quot;user::${currentUser}&quot;\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Next, in the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/src\/main\/java\/com\/couchbase\/learningpath\/data\/userprofile\/UserProfileRepository.kt#L23\"><strong>UserProfileRepository.kt<\/strong><\/a> file, locate the <code>get<\/code> method.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>override suspend fun get(currentUser: String): Map&lt;String, Any&gt; {\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: The <code>get<\/code> function is required by the interface KeyValueRepository found in the file <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/src\/main\/java\/com\/couchbase\/learningpath\/data\/KeyValueRepository.kt#L9\"><strong>KeyValueRepository.kt<\/strong><\/a>.<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We try to fetch the <code>Document<\/code> with specified <code>documentId<\/code> from the database.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>val results = HashMap&lt;String, Any&gt;()  \/\/  &lt;1&gt;\nresults[&quot;email&quot;] = currentUser as Any  \/\/  &lt;2&gt;\n\nval database = InventoryDatabase.getInstance(context).database\ndatabase?.let { db -&gt;\n  val documentId = getCurrentUserDocumentId(currentUser)\n  val doc = db.getDocument(documentId)  \/\/  &lt;3&gt;\n  if (doc != null) {\n      if (doc.contains(&quot;givenName&quot;)) { \/\/  &lt;4&gt;\n          results[&quot;givenName&quot;] = doc.getString(&quot;givenName&quot;) as Any  \/\/  &lt;4&gt;\n      }\n      if (doc.contains(&quot;surname&quot;)) { \/\/  &lt;4&gt;\n          results[&quot;surname&quot;] = doc.getString(&quot;surname&quot;) as Any  \/\/  &lt;4&gt;\n      }\n      if (doc.contains(&quot;jobTitle&quot;)) { \/\/  &lt;4&gt;\n          results[&quot;jobTitle&quot;] = doc.getString(&quot;jobTitle&quot;) as Any  \/\/  &lt;4&gt;\n      }\n      if (doc.contains(&quot;team&quot;)) { \/\/  &lt;4&gt;\n          results[&quot;team&quot;] = doc.getString(&quot;team&quot;) as Any  \/\/  &lt;4&gt;\n      }\n      if (doc.contains(&quot;imageData&quot;)) { \/\/  &lt;4&gt;\n          results[&quot;imageData&quot;] = doc.getBlob(&quot;imageData&quot;) as Any  \/\/ &lt;4&gt;\n      }\n  }\n}\nreturn@withContext results  \/\/  &lt;5&gt;\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create an instance of the <a href=\"#userprofile\">UserProfile<\/a> via <code>HashMap&lt;String, Any&gt;<\/code>.<\/li>\n\n\n<li>Set the <code>email<\/code> property of the UserProfile with the email Id of the logged-in user. This value is not editable.<\/li>\n\n\n<li>Fetch an <em>immutable<\/em> copy of the Document from the Database<\/li>\n\n\n<li>If the document exists and is fetched successfully, we use appropriate type-getters to fetch the various members of the Document based on the property name. Specifically, note the support of the <code>getBlob<\/code> type to fetch the value of a property of type <code>Blob<\/code><\/li>\n\n\n<li>Return the newly constructed <a href=\"#userprofile\">UserProfile<\/a> object.<\/li>\n\n<\/ol>\n\n\n\n<h4 id=\"h-creating-updating-a-document\" class=\"wp-block-heading\">Creating \/ Updating a Document<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">A <a href=\"#the-user-profile-document\">&#8220;User Profile&#8221; Document<\/a> is created for the user when the user taps the &#8220;Save&#8221; button on the &#8220;Edit Profile&#8221; view.  The method below applies whether you are creating a document or updating an existing version.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the <a target=\"_blank\" ref=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/android-kotlin-cbl-learning-path\/blob\/main\/src\/app\/src\/main\/java\/com\/couchbase\/learningpath\/data\/userprofile\/UserProfileRepository.kt#L54\"><em>UserProfileRepository.kt<\/em><\/a> file and locate the <code>save<\/code> function.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>override suspend fun save(data: Map&lt;String, Any&gt;): Boolean {\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We create a <strong>mutable<\/strong> instance of the Document. By default, all APIs in Couchbase Lite deal with immutable objects, thereby making them <em>thread-safe<\/em> by design.  To mutate an object, you must explicitly get a mutable copy of the object. Use appropriate type-setters to set the various properties of the Document.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>val mutableDocument = MutableDocument(documentId, data)\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Save the document.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>database?.save(mutableDocument)\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-deleting-a-document\" class=\"wp-block-heading\">Deleting a Document<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We don&#8217;t delete a <code>Document<\/code> in this sample app. However, deletion of a document is pretty straightforward and this is how you would do it.<\/p>\n\n\n\n<pre class=\"wp-block-code language-kotlin\"><code>val document = db.getDocument(documentId)\n\ndocument?.let {\n  db.delete(it)\n  result = true\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-try-it-out\" class=\"wp-block-heading\">Try It Out<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You should have followed the steps discussed in the &#8220;Try It Out&#8221; section under <a href=\"#create-and-open-a-database\">Create and Open a Database<\/a>.<\/li>\n\n\n<li>Enter a &#8220;First Name&#8221;, &#8220;Last Name&#8221;, and &#8220;Job Title&#8221; for the user in the Text Entry boxes and Tap &#8220;Save&#8221;.<\/li>\n\n\n<li>Confirm that you see a &#8220;Successfully Updated Profile&#8221; toast message. The first time you update the profile screen, the <code>Document<\/code> will be created.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"doc_create.png\" alt=\"User Profile Document Creation\" title=\"#width=250px\"><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now tap on the &#8220;Photo&#8221; image and select an image from the Photo Album.  Note that emulators don&#8217;t have photos included in most images by default, so you might need to add a photo to select.  Also some versions of Android differ in the UI used to browse for images.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"image_selection.gif\" alt=\"image selection\"><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Tap &#8220;Save&#8221;<\/li>\n\n\n<li>Confirm that you see the toast message &#8220;Successfully Updated Profile&#8221;. The <code>Document<\/code> will be updated this time.<\/li>\n\n\n<li>Tap the Drawer menu icon in the upper left hand corner<\/li>\n\n\n<li>Tap &#8220;Logout&#8221; to log out of the app<\/li>\n\n\n<li>Log back into the app with the same user email Id and password that you used earlier. In my example, I used <strong>&#8220;<a href=\"mailto:demo@example.com\">demo@example.com<\/a>&#8220;<\/strong> and <strong>&#8220;P@ssw0rd12&#8221;<\/strong>. (Logging in with those credentials again)<\/li>\n\n\n<li>Tap the Drawer menu icon in the upper left hand corner again<\/li>\n\n\n<li>Confirm that you see the user profile widget with the <em>name<\/em> and <em>image<\/em> values that you set earlier.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"log_off_on.gif\" alt=\"log off\"><\/p>\n\n\n\n<h3 id=\"h-learn-more\" class=\"wp-block-heading\">Learn More<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations on completing the first step of the learning path by reviewing the code that saves User Profiles!  In this section, we walked through a very basic example of how to get up and running with Couchbase Lite as a local-only, standalone embedded data store in your Android app.  Continue on to the next step to learn how to use a pre-built database with Couchbase Lite.<\/p>\n\n\n\n<h4 id=\"h-references\" class=\"wp-block-heading\">References<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.couchbase.com\/couchbase-lite\/current\/android\/gs-install.html\">Documentation: Installing Couchbase Lite on Android<\/a><\/li>\n\n\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.couchbase.com\/couchbase-lite\/current\/android\/database.html\">Documentation: Database for Android<\/a><\/li>\n\n\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.couchbase.com\/couchbase-lite\/current\/android\/document.html\">Documentation:  Documents &#8211; Data Model<\/a><\/li>\n\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this part of our learning path, you will walk through the &#8220;Audit Inventory&#8221; demo application of using Couchbase Lite in standalone mode, which means the database does not sync information from other resources using replication. While the demo app has a lot of functionality, this step will walk you through: In this step [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false,"inline_featured_image":false,"_acf":"","footnotes":""},"tutorial-languages":[589],"tutorial-tags":[591],"tutorial-technologies":[548,585],"tutorial-types":[555],"class_list":["post-10261","tutorials","type-tutorials","status-publish","hentry","tutorial-languages-dart","tutorial-tags-flutter","tutorial-technologies-kv","tutorial-technologies-mobile","tutorial-types-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Learn Couchbase Lite Key Value Engine with Dart and Flutter - Couchbase<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/pt\/developers\/tutorials\/dart-flutter-learning-key-value\/\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learn Couchbase Lite Key Value Engine with Dart and Flutter\" \/>\n<meta property=\"og:description\" content=\"Introduction In this part of our learning path, you will walk through the &#8220;Audit Inventory&#8221; demo application of using Couchbase Lite in standalone mode, which means the database does not sync information from other resources using replication. While the demo app has a lot of functionality, this step will walk you through: In this step [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/pt\/developers\/tutorials\/dart-flutter-learning-key-value\/\" \/>\n<meta property=\"og:site_name\" content=\"Couchbase\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Couchbase\/\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/10\/Cloud-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2548\" \/>\n\t<meta property=\"og:image:height\" content=\"1171\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@couchbase\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"10 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/developers\\\/tutorials\\\/dart-flutter-learning-key-value\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/developers\\\/tutorials\\\/dart-flutter-learning-key-value\\\/\",\"name\":\"Learn Couchbase Lite Key Value Engine with Dart and Flutter - Couchbase\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/#website\"},\"datePublished\":\"2023-06-23T08:43:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/developers\\\/tutorials\\\/dart-flutter-learning-key-value\\\/#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/developers\\\/tutorials\\\/dart-flutter-learning-key-value\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/developers\\\/tutorials\\\/dart-flutter-learning-key-value\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.couchbase.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Learn Couchbase Lite Key Value Engine with Dart and Flutter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/#website\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/\",\"name\":\"Couchbase\",\"description\":\"The Operational Data Platform for AI\u2122\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/#organization\"},\"alternateName\":\"Couchbase, Inc.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/#organization\",\"name\":\"Couchbase\",\"alternateName\":\"Couchbase, Inc.\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"contentUrl\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"width\":1129,\"height\":1129,\"caption\":\"Couchbase\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/pt\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Couchbase\\\/\",\"https:\\\/\\\/x.com\\\/couchbase\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/CouchbaseServer\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/couchbase\\\/\",\"https:\\\/\\\/github.com\\\/couchbase\\\/\",\"https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Couchbase,_Inc.\"],\"description\":\"Couchbase is a database software company headquartered in San Jose, California, providing an operational data platform for AI used by hundreds of global enterprises.\",\"legalName\":\"Couchbase, Inc.\",\"foundingDate\":\"2011-01-01\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Learn Couchbase Lite Key Value Engine with Dart and Flutter - Couchbase","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/pt\/developers\/tutorials\/dart-flutter-learning-key-value\/","og_locale":"pt_BR","og_type":"article","og_title":"Learn Couchbase Lite Key Value Engine with Dart and Flutter","og_description":"Introduction In this part of our learning path, you will walk through the &#8220;Audit Inventory&#8221; demo application of using Couchbase Lite in standalone mode, which means the database does not sync information from other resources using replication. While the demo app has a lot of functionality, this step will walk you through: In this step [&hellip;]","og_url":"https:\/\/www.couchbase.com\/pt\/developers\/tutorials\/dart-flutter-learning-key-value\/","og_site_name":"Couchbase","article_publisher":"https:\/\/www.facebook.com\/Couchbase\/","og_image":[{"width":2548,"height":1171,"url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/10\/Cloud-2.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@couchbase","twitter_misc":{"Est. reading time":"10 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/pt\/developers\/tutorials\/dart-flutter-learning-key-value\/","url":"https:\/\/www.couchbase.com\/pt\/developers\/tutorials\/dart-flutter-learning-key-value\/","name":"Learn Couchbase Lite Key Value Engine with Dart and Flutter - Couchbase","isPartOf":{"@id":"https:\/\/www.couchbase.com\/pt\/#website"},"datePublished":"2023-06-23T08:43:43+00:00","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/pt\/developers\/tutorials\/dart-flutter-learning-key-value\/#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/pt\/developers\/tutorials\/dart-flutter-learning-key-value\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/pt\/developers\/tutorials\/dart-flutter-learning-key-value\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/"},{"@type":"ListItem","position":2,"name":"Learn Couchbase Lite Key Value Engine with Dart and Flutter"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/pt\/#website","url":"https:\/\/www.couchbase.com\/pt\/","name":"Couchbase","description":"The Operational Data Platform for AI\u2122","publisher":{"@id":"https:\/\/www.couchbase.com\/pt\/#organization"},"alternateName":"Couchbase, Inc.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/pt\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/pt\/#organization","name":"Couchbase","alternateName":"Couchbase, Inc.","url":"https:\/\/www.couchbase.com\/pt\/","logo":{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/www.couchbase.com\/pt\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","contentUrl":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","width":1129,"height":1129,"caption":"Couchbase"},"image":{"@id":"https:\/\/www.couchbase.com\/pt\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Couchbase\/","https:\/\/x.com\/couchbase","https:\/\/www.youtube.com\/c\/CouchbaseServer","https:\/\/www.linkedin.com\/company\/couchbase\/","https:\/\/github.com\/couchbase\/","https:\/\/en.wikipedia.org\/wiki\/Couchbase,_Inc."],"description":"A Couchbase \u00e9 uma empresa de software de banco de dados com sede em San Jose, Calif\u00f3rnia, que fornece uma plataforma de dados operacional para a intelig\u00eancia artificial utilizada por centenas de empresas globais.","legalName":"Couchbase, Inc.","foundingDate":"2011-01-01"}]}},"acf":[],"permalink_manager":null,"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/pt\/wp-json\/wp\/v2\/tutorials\/10261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/pt\/wp-json\/wp\/v2\/tutorials"}],"about":[{"href":"https:\/\/www.couchbase.com\/pt\/wp-json\/wp\/v2\/types\/tutorials"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/pt\/wp-json\/wp\/v2\/tutorials\/10261\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/pt\/wp-json\/wp\/v2\/media?parent=10261"}],"wp:term":[{"taxonomy":"tutorial-languages","embeddable":true,"href":"https:\/\/www.couchbase.com\/pt\/wp-json\/wp\/v2\/tutorial-languages?post=10261"},{"taxonomy":"tutorial-tags","embeddable":true,"href":"https:\/\/www.couchbase.com\/pt\/wp-json\/wp\/v2\/tutorial-tags?post=10261"},{"taxonomy":"tutorial-technologies","embeddable":true,"href":"https:\/\/www.couchbase.com\/pt\/wp-json\/wp\/v2\/tutorial-technologies?post=10261"},{"taxonomy":"tutorial-types","embeddable":true,"href":"https:\/\/www.couchbase.com\/pt\/wp-json\/wp\/v2\/tutorial-types?post=10261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}