TL;DR:有没有一种方法可以监听流并使用Cubit而不是发出状态BLoC?
我正在使用BLoCFlutter 库,我们使用Cubits 进行状态管理。到目前为止,获取数据或保存数据的交互一切正常,但现在我需要使用流。就我而言,这意味着观看来自 的快照FirebaseFirestore。
我尝试在互联网上搜索是否有方法观察流而Cubit不是使用BLoC,但大多数结果都指向我BLoC。我曾在另一个项目中工作过BLoC,所以我知道如何使用它来观察流,但Cubit如果有办法的话我想使用它。
以下是我在 FireStore 中观察的代码示例:
@override
Stream<Either<Failure, List<MTalk>>> watchTalk() async* {
const path ='path/to/talks';
yield* firestore
.collection(path)
.snapshots()
.map(
(snap) => right<Failure, List<MTalk>>(
snap.docs
.map(
(documentSnapshot) => MTalk.fromFirestore(documentSnapshot))
.toList(),
),
)
.onErrorReturnWith((e) {
if (e is FirestoreException) {
return left(RetrieveFailure(message: e.message));
} else {
return left(UnknownFailure(message: e.toString()));
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
使用时BLoC,只要从调用返回数据,您就可以简单地使用async* …
C#中的列表.Append()与.Add()方法之间是否存在差异?我尝试在谷歌和网站上搜索,但令我惊讶的是没有人问过它.我的理由是要知道这两种方法中的一种是否性能较低.我一直在交替使用这两种方法,我没有看到它们的功能有任何差异,因为它们都将我的对象添加到列表的末尾(这或多或少是视觉工作室在你看到时给你的描述)方法描述).
编辑:
嗯,我一开始并不认为这很重要,但我发现当我无法在控制台应用程序上执行此操作时,我可以在ASP.NET Web应用程序上使用Append.所以我在ASP.NET Web App的上下文中提出这个问题.抱歉这是混乱.
我想打完电话后执行一些代码collect上Flow<MyClass>。我对使用 Flows 还是有点陌生,所以我不明白为什么函数后面的代码没有被调用。
我如何使用流程:
incidentListener = FirebaseUtils.databaseReference
.child(AppConstants.FIREBASE_PATH_AS)
.child(id)
.listen<MyClass>() //This returns a Flow<MyClass?>?
Run Code Online (Sandbox Code Playgroud)
我如何消耗流量:
private suspend fun myFun() {
viewmodel.getListener()?.collect { myClass->
//do something here
}
withContext(Dispatchers.Main) { updateUI() } //the code never reaches this part
}
Run Code Online (Sandbox Code Playgroud)
怎么myFun()称呼:
CoroutineScope(Dispatchers.IO).launch {
myFun()
}
Run Code Online (Sandbox Code Playgroud)
至于我试图让它工作的东西,我已经尝试关闭协程上下文,但它没有用。我假设 Flows 的工作方式与常规协程不同。
更新:
我正在使用此代码块通过 Firebase 进行侦听。我不知道它是否会有所帮助,但也许我实施它的方式导致了问题?
inline fun <reified T> Query.listen(): Flow<T?>? =
callbackFlow {
val valueListener = object : ValueEventListener {
override fun onCancelled(databaseError: DatabaseError) {
close()
}
override …Run Code Online (Sandbox Code Playgroud) 嗨,我是Android开发的新手,我正在尝试在YouTube上找到的有关MVVM的教程。视频中的示例项目使用AppCompat,但我将自己的代码转换为androidx,因为从我读取的内容中可以使用current(?)版本?我是否对这种想法感到误解?
无论如何,本教程的一部分利用了RecyclerView,我无法在我的activity_main.xml文件中访问它,表明v7是一个未解决的程序包。android.support.v7.widget.RecyclerView从v7开始以红色文本显示。我知道我可以将其还原为旧版本,但我想我正在努力使这项工作取得成功,因为它有望了解如何正确使用androidx?
我不知道如何在当前项目迁移到androidx的情况下将RecyclerView添加到项目中。
我尝试过的
implementation 'com.android.support:recyclerview-v7:28.0.0'根据文档添加我的依存关系:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
//RecyclerView
implementation 'com.android.support:recyclerview-v7:28.0.0'
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0-alpha04"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.1.0-alpha04"
// Room Components
implementation "androidx.room:room-runtime:2.1.0-alpha06"
annotationProcessor "androidx.room:room-compiler:2.1.0-alpha06"
}
Run Code Online (Sandbox Code Playgroud)
我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<view class="android.support.v7.widget.RecyclerView"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/todo_item"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud) 我正在尝试调整选项卡的间距,以便它在停靠在底部中心的浮动操作按钮附近看起来不会很尴尬。我正在考虑添加一个“隐形”图标,但这意味着用户可能会错误地点击它,我认为这不是最好的解决方法。
我的小部件:
Widget build(context) {
return new Scaffold(
body: TabBarView(
children: _displayTabPages(),
),
bottomNavigationBar: BottomAppBar(
child: _createTabBar(),
color: Colors.blue,
shape: CircularNotchedRectangle(),
notchMargin: 5.0,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Add Transaction',
backgroundColor: Colors.orange[300],
foregroundColor: Colors.black87,
child: Icon(FontAwesomeIcons.plus),
elevation: 2.0,
),
);
}
Run Code Online (Sandbox Code Playgroud)
我的标签栏:
Widget _createTabBar() {
return TabBar(
tabs: [
Tab(
icon: new Icon(FontAwesomeIcons.list),
),
Tab(
icon: new Icon(FontAwesomeIcons.calendarAlt),
),
Tab(
icon: new Icon(FontAwesomeIcons.chartPie),
),
Tab(
icon: new Icon(FontAwesomeIcons.cog),
)
],
labelColor: Colors.white,
unselectedLabelColor: Colors.black87,
indicatorColor: Colors.blue, …Run Code Online (Sandbox Code Playgroud) 因此,我们已将 Firebase Analytics 和 Crashlytics 与我们的移动应用(Android 和 iOS)集成,并且可以正常工作。嗯,它有点工作。因此,我们对 crashlytics 没有任何问题,但我们在如何报告自定义事件方面存在问题。
如果您看到上图,我们将记录大量(未设置)。我们不会发送任何未记录的自定义内容(意味着我们选择记录的所有大蓝框),但我们仍然会收到(未设置)报告。有没有办法知道这是什么或如何删除它?我一开始以为会是firebase_event_origin或类似的东西,但事实并非如此,因为在我们在参数报告中添加了上述属性后,数字仍在不断增加。
为了将数据发送到 firebase 分析,我们只是做了文档中概述的内容,例如:
Bundle params = new Bundle();
params.putString("image_name", name);
params.putString("full_text", text);
mFirebaseAnalytics.logEvent("share_image", params);
Run Code Online (Sandbox Code Playgroud) 如何删除 Flutter 项目中的 Web 组件部分?我有一个项目,它在创建时是通过网络支持自动创建的。现在我 100% 确定该应用程序不会用于网络,我只想删除对它的支持。终端中有命令可以帮助我做到这一点吗?
我尝试检查其他问题,但其他人询问如何完全禁用网络支持。我只想删除一个项目的网络支持。