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.mzph.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…