我有一个包含 2 个页面的应用程序,当点击主页按钮时,它会导航到“设置”页面。
IconButton(
onPressed:(){
Navigator.push(
context,
MaterialPageRoute( builder: (context) => Settings())
);
},
),
但是在设置页面上,来 self 的 bloc 的流数据都是 NULL。我不知道为什么,在主页上一切都很好。
我在设置页面的 Bloc 中添加了新数据作为测试,它显示正常,所以 Bloc 实现工作正常,只是没有在 bloc 构造函数中获取初始化数据。
我做错了什么?
Bloc 数据在 bloc 构造函数中初始化,这是我的 Bloc 代码
class StateBloc implements BlocBase {
var selectedCurrencies;
StreamController<List> _selectedCurrencies = StreamController<List>.broadcast();
Stream<List> get getSelectedCurrencies => _selectedCurrencies.stream;
StreamSink get modifySelectedCurrencies => _selectedCurrencies.sink;
StateBloc() {
selectedCurrencies = ['cny','aud','usd'];
modifySelectedCurrencies.add(selectedCurrencies);
}
}
这里是如何将 bloc 提供给 main.dart
Future<void> main() async{
return runApp(
BlocProvider<StateBloc>(
child: MyApp(),
bloc: StateBloc(),
),
);
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Exchange',
debugShowCheckedModeBanner: false,
theme: ThemeData(fontFamily: 'Poppins'),
home: MyHomePage(),
);
}
}
这是我的设置页面
class Settings extends StatelessWidget {
@override
Widget build(BuildContext context) {
final StateBloc appBloc = BlocProvider.of<StateBloc>(context);
return MaterialApp(
home: Scaffold(
body: ListView(
children: <Widget>[
StreamBuilder(
stream: appBloc.getSelectedCurrencies,
builder: (context, selectedCurrenciesSnap) {
print('alllll --> ${allCurrenciesSnap.data}');
}
)
]
)
)
)
}
}
最佳答案
我发现了问题。这是注定要发生的,当build设置页面中的 StreamBuilder 时,它只监听即将到来的数据,而不是之前的数据。为了获得以前的数据,我们需要重构一些东西。
在 Bloc 中,使用 rxdart 包中的 BehaviorSubject 而不是 streamController,并定义 ValueObservable 而不是 Stream
StreamController _selectedCurrencies = BehaviorSubject();
ValueObservable get getSelectedCurrencies => _selectedCurrencies.stream;
StreamSink get modifySelectedCurrencies => _selectedCurrencies.sink;
然后在“设置”页面中,为 streamBuilder 提供初始数据
initialData: appBloc.getSelectedCurrencies.value,
这有效:)
关于dart - flutter Bloc pattern -- 如何在导航器之后访问 bloc 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54263716/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121