flutter bloc_如何在Flutter中使用Streams,BLoC和SQLite

flutter bloc

Recently, I’ve been working with streams and BLoCs in Flutter to retrieve and display data from an SQLite database. Admittedly, it took me a very long time to make sense of them. With that said, I’d like to go over all this in hopes you’ll walk away feeling confident in using them within your own apps. I’ll be going into as much depth as I possibly can and explaining everything as simply as possible.

最近,我一直在Flutter中使用流和BLoC來從SQLite數據庫檢索和顯示數據。 誠然,我花了很長時間才理解它們。 話雖如此,我想復習所有這一切,希望您能自信地在自己的應用程序中使用它們。 我將盡可能深入,并盡可能簡單地解釋所有內容。

In this post, we’ll be making a simple app from start to finish that makes use of streams, BLoCs, and an SQLite database. This app will allow us to create, modify, and delete notes. If you haven’t done so yet, create a new barebones Flutter app using flutter create APPNAME. It'll be a lot easier to understand all this if you start fresh. Then, later on, implement what you learned into your existing apps.

在本文中,我們將從頭到尾制作一個簡單的應用程序,該應用程序將使用流,BLoC和SQLite數據庫。 這個程序將允許我們創建,修改和刪除注釋。 如果尚未執行,請使用flutter create APPNAME創建一個新的Flutter應用程序。 如果您重新開始,那么了解這一切會容易得多。 然后,稍后,將您學到的知識應用到現有應用中。

The first order of business is creating a class to handle the creation of our tables and to query the database. To do this properly, we need to add sqflite and path_provider as dependencies in our pubspec.yaml file.

首先要創建一個類來處理表的創建和查詢數據庫。 為了正確執行此操作,我們需要在我們的pubspec.yaml文件中添加sqflitepath_provider作為依賴pubspec.yaml

In case it doesn’t run automatically, run flutter packages get to retrieve the packages. Once it finishes, create a data folder and a database.dart file within it. This class will create a singleton so we can access the database from other files, open the database, and run queries on that database. I've included comments to explain some of the code.

如果它沒有自動運行,請運行flutter packages get以檢索軟件包。 完成后,在其中創建一個data文件夾和一個database.dart文件。 此類將創建一個單例,以便我們可以從其他文件訪問數據庫,打開數據庫并在該數據庫上運行查詢。 我添加了注釋來解釋一些代碼。

Create another folder, models, and add one file to it: note_model.dart. Here's a great tool to easily make models: https://app.quicktype.io/#l=dart.

創建另一個文件夾, models并向其中添加一個文件: note_model.dart 。 這是一個輕松制作模型的好工具: https : //app.quicktype.io/#l=dart 。

NOTE: Keep in mind that models do NOT have to copy the columns in the table. For example, if you have a user id stored in a table as a foreign key, the model probably shouldn’t contain that user id. Instead, the model should use that user id in order to retrieve an actual User object.

注意:請記住,模型不必復制表中的列。 例如,如果您有一個用戶ID作為外鍵存儲在表中,則該模型可能不應包含該用戶ID。 相反,模型應使用該用戶ID來檢索實際的User對象。

With our note model created, we can add the final functions to our database file that’ll handle all note related queries.

創建便箋模型后,我們可以將最終功能添加到數據庫文件中,以處理所有便箋相關查詢。

Let’s get started with streams and BLoCs now. If this is your first time working with these, it can be quite daunting. I promise you though that streams and BLoCs are exceptionally simple once you get past the learning phase.

現在讓我們開始使用流和BLoC。 如果這是您第一次使用這些工具,那可能會很艱巨。 我向您保證,一旦您超過了學習階段,流和BLoC就會異常簡單。

The first thing we need is a blocs folder within the data folder. This folder will contain all our BLoCs, as the name suggests. Let's create the files for each BLoC: bloc_provider.dart, notes_bloc.dart, and view_note_bloc.dart. One BLoC per page and one to provide the BLoCs to those pages.

我們需要的第一件事是data文件夾中的blocs文件夾。 顧名思義,此文件夾將包含我們所有的BLoC。 讓我們為每個BLoC創建文件: bloc_provider.dartnotes_bloc.dartview_note_bloc.dart 。 每頁一個BLoC,一個為這些頁面提供BLoC。

The bloc_provider is in charge of easily providing our pages with the necessary BLoC and then disposing of it when necessary. Every time we want to use a BLoC, we'll be using the bloc_provider.

bloc_provider負責輕松地為我們的頁面提供必要的BLoC,然后在必要時進行處理。 每次我們想要使用BLoC時,我們都會使用bloc_provider

Whenever we need a BLoC on one of our pages, we’ll utilize the BlocProvider like this:

每當我們在頁面之一上需要BLoC時,我們都將像這樣使用BlocProvider

Let’s create our notes BLoC which will handle retrieving all our notes and adding new notes to the database. Since our BLoCs are page specific, this BLoC will only be used on the notes page. I’ve commented the code to explain what’s going on.

讓我們創建便箋BLoC,它將處理檢索所有便箋并將新便箋添加到數據庫的過程。 由于我們的BLoC特定于頁面,因此該BLoC僅在注釋頁面上使用。 我已經注釋了代碼以解釋正在發生的事情。

With the notes BLoC created, we have everything we need to create our notes page. This page will display all our notes, and allow us to add new ones. We’ll put the code for our notes page into main.dart. Once again, I've commented on all the necessary pieces of code to explain what's going on.

創建BLoC便箋后,我們擁有創建便箋頁面所需的一切。 此頁面將顯示我們所有的注釋,并允許我們添加新的注釋。 我們將注釋頁面的代碼放入main.dart 。 我再次評論了所有必要的代碼段,以解釋正在發生的事情。

Now we need a way to view, edit, save, and delete the notes. This is where the view note BLoC and the view note page come into play. We’ll start with view_note_bloc.dart.

現在,我們需要一種查看,編輯,保存和刪除注釋的方法。 這是視圖注釋BLoC和視圖注釋頁面起作用的地方。 我們將從view_note_bloc.dart開始。

Now we can build the actual page to allow us to interact with our notes. The code for this page is going in view_note.dart.

現在,我們可以構建實際頁面,以允許我們與筆記進行交互。 該頁面的代碼在view_note.dart

That’s all it takes to work with streams, BLoCs, and SQLite. Using them, we’ve created a super simple app that allows us to create, view, edit, and delete notes. I hope this walkthrough has made you more confident in working with streams. You’ll now be able to implement them into your own apps with ease. If you have any questions, please leave a comment as I’d love to answer them. Thanks for reading.

這就是使用流,BLoC和SQLite所需要的全部。 使用它們,我們創建了一個超級簡單的應用程序,使我們可以創建,查看,編輯和刪除便箋。 我希望本演練使您對使用流更加自信。 現在,您可以輕松地將它們實現到自己的應用程序中。 如有任何疑問,請留下評論,我很樂意回答。 謝謝閱讀。

View the full code here: https://github.com/Erigitic/flutter-streams

在此處查看完整的代碼: https : //github.com/Erigitic/flutter-streams

翻譯自: https://www.freecodecamp.org/news/using-streams-blocs-and-sqlite-in-flutter-2e59e1f7cdce/

flutter bloc

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391686.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391686.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391686.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

leetcode 303. 區域和檢索 - 數組不可變

給定一個整數數組 nums,求出數組從索引 i 到 j(i ≤ j)范圍內元素的總和,包含 i、j 兩點。 實現 NumArray 類: NumArray(int[] nums) 使用數組 nums 初始化對象 int sumRange(int i, int j) 返回數組 nums 從索引 i …

Bigmart數據集銷售預測

Note: This post is heavy on code, but yes well documented.注意:這篇文章講的是代碼,但確實有據可查。 問題描述 (The Problem Description) The data scientists at BigMart have collected 2013 sales data for 1559 products across 10 stores in…

Android控制ScrollView滑動速度

翻閱查找ScrollView的文檔并搜索了一下沒有發現直接設置的屬性和方法,這里通過繼承來達到這一目的。 /*** 快/慢滑動ScrollView * author農民伯伯 * */public class SlowScrollView extends ScrollView {public SlowScrollView(Context context, Att…

數據特征分析-帕累托分析

帕累托分析(貢獻度分析):即二八定律 目的:通過二八原則尋找屬于20%的關鍵決定性因素。 隨機生成數據 df pd.DataFrame(np.random.randn(10)*10003000,index list(ABCDEFGHIJ),columns [銷量]) #避免出現負數 df.sort_values(銷量,ascending False,i…

leetcode 304. 二維區域和檢索 - 矩陣不可變(前綴和)

給定一個二維矩陣,計算其子矩形范圍內元素的總和,該子矩陣的左上角為 (row1, col1) ,右下角為 (row2, col2) 。 上圖子矩陣左上角 (row1, col1) (2, 1) ,右下角(row2, col2) (4, 3),該子矩形內元素的總和為 8。 示…

算法訓練營 重編碼_編碼訓練營后如何找到工作

算法訓練營 重編碼by Roxy Ayaz由Roxy Ayaz 編碼訓練營后如何找到工作 (How to get a job after a coding bootcamp) Getting a tech job after a coding bootcamp is very possible, but not necessarily pain-free.在編碼訓練營之后獲得技術工作是很有可能的,但不…

dt決策樹_決策樹:構建DT的分步方法

dt決策樹介紹 (Introduction) Decision Trees (DTs) are a non-parametric supervised learning method used for classification and regression. The goal is to create a model that predicts the value of a target variable by learning simple decision rules inferred f…

讀C#開發實戰1200例子記錄-2017年8月14日10:03:55

C# 語言基礎應用,注釋 "///"標記不僅僅可以為代碼段添加說明,它還有一項更重要的工作,就是用于生成自動文檔。自動文檔一般用于描述項目,是項目更加清晰直觀。在VisualStudio2015中可以通過設置項目屬性來生成自動文檔。…

iOS端(騰訊Bugly)閃退異常上報撲獲日志集成與使用指南

app已經上架并且有三次更新版本,今天市場部和顧客約談時,發現顧客的iphone 6 plus iOS 9.0.2上運行app點擊登錄按鈕時直接閃退,無法進入app里,這個問題還是第一次遇到,我下載了相應的模擬器版本, 并在上面運…

數據特征分析-正太分布

期望值,即在一個離散性隨機變量試驗中每次可能結果的概率乘以其結果的總和。 若隨機變量X服從一個數學期望為μ、方差為σ^2的正態分布,記為N(μ,σ^2),其概率密度函數為正態分布的期望值μ決定了其位置,其標準差σ決定…

leetcode 338. 比特位計數

給定一個非負整數 num。對于 0 ≤ i ≤ num 范圍中的每個數字 i ,計算其二進制數中的 1 的數目并將它們作為數組返回。 示例 1: 輸入: 2 輸出: [0,1,1] 示例 2: 輸入: 5 輸出: [0,1,1,2,1,2] 解題思路 偶數:和偶數除以2以后的數字,具有相…

r語言調用數據集中的數據集_自然語言數據集中未解決的問題

r語言調用數據集中的數據集Garbage in, garbage out. You don’t have to be an ML expert to have heard this phrase. Models uncover patterns in the data, so when the data is broken, they develop broken behavior. This is why researchers allocate significant reso…

為什么不應該使用(長期存在的)功能分支

Isn’t the git history in the picture above nice to work with? There are probably many problems in that repository, but one of them is most definitely the use of feature branches. Let’s see how such a bad thing became such a common practice.上面圖片中的g…

(轉) Spring 3 報org.aopalliance.intercept.MethodInterceptor問題解決方法

http://blog.csdn.net/henuhaigang/article/details/13678023 轉自CSDN博客,因為一個jar包沒引入困擾我好長時間 ,當時正在做spring AOP 的一個小測試,總在報錯,最后發現自己是問題3,引入一個jar包得到了解決 一 開發環…

數據特征分析-相關性分析

相關性分析是指對兩個或多個具備相關性的變量元素進行分析,從而衡量兩個變量的相關密切程度。 相關性的元素之間需要存在一定的聯系或者概率才可以進行相關性分析。 相關系數在[-1,1]之間。 一、圖示初判 通過pandas做散點矩陣圖進行初步判斷 df1 pd.DataFrame(np.…

leetcode 354. 俄羅斯套娃信封問題(dp+二分)

給定一些標記了寬度和高度的信封,寬度和高度以整數對形式 (w, h) 出現。當另一個信封的寬度和高度都比這個信封大的時候,這個信封就可以放進另一個信封里,如同俄羅斯套娃一樣。 請計算最多能有多少個信封能組成一組“俄羅斯套娃”信封&#…

fastlane use_legacy_build_api true

fastlane版本號:fastlane 1.108.0 Xcode版本號:8.1 MacOS版本號:10.12 使用fastlane打包 - Release / Ad-Hoc包時報錯: [13:36:59]: There was an error exporting your application [13:36:59]: Unfortunately the new Xcode export API is …

獲取所有權_住房所有權經濟學深入研究

獲取所有權Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seekin…

scala 單元測試_Scala中的法律測試簡介

scala 單元測試Property-based law testing is one of the most powerful tools in the scala ecosystem. In this post, I’ll explain how to use law testing and the value it’ll give you using in-depth code examples.基于財產的法律測試是scala生態系統中最強大的工具…

getBoundingClientRect說明

getBoundingClientRect用于獲取某個元素相對于視窗的位置集合。 1.語法:這個方法沒有參數。 rectObject object.getBoundingClientRect() 2.返回值類型:TextRectangle對象,每個矩形具有四個整數性質( 上, 右 &#xf…