您好,我想将实时数据库中的快照反序列化为Company 对象,并将其添加到_companies 列表中。 但是我一直收到错误...
这是我目前所拥有的:
List<Company> _companies = [];
@override
void initState() {
// TODO: implement initState
super.initState();
getItems().then((list){
print("Now the list is here");
setState(() {
for (int i=0; i < list.length; i++) {
Map<String, dynamic> map = list[i];
Company company = new Company.fromMap(map);
_companies.add(company);
}
});
});
}
static Future<List> getItems( ) async {
Completer<List> completer = new Completer<List>();
FirebaseDatabase.instance
.reference()
.child("Companies")
.once()
.then( (DataSnapshot snapshot) {
List map = snapshot.value; //It fails here
completer.complete(map);
} );
return completer.future;
}
这是我的公司类:
class Company {
String key;
String category;
String company_name;
String company_url;
String country;
String description;
String email;
String faq_url;
String instagram_url;
String logo_url_image;
String p_category;
String parent_company_ok;
String timestamp;
Company();
Company.fromSnapshot(DataSnapshot snapshot)
: key = snapshot.key,
category = snapshot.value['category'],
company_name = snapshot.value['company_name'],
company_url = snapshot.value['company_url'],
country = snapshot.value['country'],
description= snapshot.value['description'],
email = snapshot.value['email'],
faq_url = snapshot.value['faq_url'],
instagram_url = snapshot.value['instagram_url'],
logo_url_image = snapshot.value['logo_url_image'],
p_category = snapshot.value['p_category'],
parent_company_ok = snapshot.value['parent_company_ok'],
timestamp = snapshot.value['timestamp'];
}
然而,它在 List map = snapshot.value; 上的 getItems( ) 中失败了.异常(exception)情况:_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List<dynamic>'
此外,如果有人可以提供在列表小部件中显示子项的代码,我将非常高兴:)
这是我在 firebase 实时数据库中的数据结构:
最佳答案
因为错误消息说 snapshot.value 不是一个列表,而是一个映射,所以行 List map = snapshot.value; 总是会失败。由于您正在从数据库中读取 Companies 节点树,因此这将返回 map 的 map ,类似于 Map<String, Map<dynamic,dynamic>>。来自各个节点的所有数据。我将为您提供一个函数来解析 Company 对象中的 Json 数据,我将在源代码中留下一些注释以尝试向您解释该过程。很简单。
List<Company> _parseData(DataSnapshot dataSnapshot) {
List<Company> companyList =new List();
// here you replace List map = snapshot.value with... dataSnapshot.val()
Map<String, dynamic> mapOfMaps = Map.from( dataSnapshot.val() );
//actually dynamic here is another Map, String are the keys like -LXuQmyuF7E... or LXuSkMdJX...
//of your picture example and dynamic other Map
//in next lines we will interate in all values of your map
//remeber that the values are maps too, values has one map for each company.
//The values are Map<String, dynamic>
//in this case String are keys like category, company_name, country, etc...
//and dynamics data like string, int, float, etc
//parsing and adding each Company object to list
mapOfMaps.values.forEach( (value) {
companyList.add(
//here you'll not use fromSnapshot to parse data,
//i thing you got why we're not using fromSnapshot
Company.fromJson( Map.from(value) )
);
});
return companyList;
}
请注意,当您从中读取特定公司时,您将使用 Company.fromSnapshot(snapshot) 你的数据库,比如...
// just to illustrate
var snapshot = await FirebaseDatabase.instance
.reference()
.child("Companies")
.child(companyId).once();
Company.fromSnapshot( snapshot );
因为在这种情况下 snapshot.value 是单个 map 。 好吧,另一件事是,看看你的 statefull 小部件中的 initState 方法,我不知道这是否是一个好方法 即使在 Future 执行之后,也可以在 initState 方法中调用 setState。这只是一个建议。
关于firebase - Flutter firebase_database 得到 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54699756/
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
如标题所示,我正在尝试使用Rspec测试自定义验证器。我得到一个错误,我不明白为什么......如果你能阐明一些问题,我将非常感激。我们开始吧:验证者规范require'spec_helper'describeGraphDateValidatordoit"shouldnotvalidateactivitywithemptystarttime"doexpect{Graph.new({start_time:''}).valid?}.toeq(false)endend如果我打印Graph.new({start_time:''}).valid?它会打印false然而,当它通过规范时,它返回一个
我有这个ruby代码:defget_sumnreturn0ifn似乎正在为999之前的值工作。当我尝试9999时,它给了我这个:stackleveltoodeep(SystemStackError)所以,我添加了这个:RubyVM::InstructionSequence.compile_option={:tailcall_optimization=>true,:trace_instruction=>false}但什么也没发生。我的ruby版本是:ruby1.9.3p392(2013-02-22revision39386)[x86_64-darwin12.2.1]我还增加了机器的堆栈大
为了防止在迁移到生产站点期间出现数据库事务错误,我们遵循了https://github.com/LendingHome/zero_downtime_migrations中列出的建议。(具体由https://robots.thoughtbot.com/how-to-create-postgres-indexes-concurrently-in概述),但在特别大的表上创建索引期间,即使是索引创建的“并发”方法也会锁定表并导致该表上的任何ActiveRecord创建或更新导致各自的事务失败有PG::InFailedSqlTransaction异常。下面是我们运行Rails4.2(使用Acti
我尝试在IRB(v0.9.6,Ruby2.3.0)中使用Refinement:moduleFoorefineObjectdodeffoo()"foo"endendendusingFoo#=>RuntimeError:main.usingispermittedonlyattoplevel这基本上是theexactsetupfromthedocumentation(这会导致相同的错误)。出了什么问题?我该如何解决这个问题? 最佳答案 这可能是IRb的错误或功能不当。众所周知,由于IRb的实现方式非常骇人听闻,因此它无法在所有极端情况下正
这个问题在这里已经有了答案:Howtoevaluateadatedifferenceinyears,monthsanddays(ruby)?(6个答案)关闭8年前。如何获取以days,hours,mins为单位的时差我正在努力datetime_A-datetime_Bdatetime_A=2014年1月4日星期六07:00:13+0000datetime_B=2014年1月3日星期五01:09:46+0000它返回类似(35809/28800)的东西,bdw这是什么意思?我需要像1day,5h,23min怎么做到的?
我正在使用最新版本的Capistrano和我的Rails4应用程序。运行capdpeloy时。我得到了很多输出,包括这次失败:DEBUG[04b6e226]Running/usr/bin/env[-f/var/www/skateboxes/releases/20131022135522/config/database.yml]on162.243.33.179DEBUG[04b6e226]Command:[-f/var/www/skateboxes/releases/20131022135522/config/database.yml]DEBUG[04b6e226]Finishedin0
我想创建一个RailsController,从网上下载一系列jpg文件,并直接将它们作为二进制文件写入数据库(我不是要上传表格)关于如何做到这一点的任何线索?谢谢编辑:这是我已经使用attachment-fugem编写的一些代码:http=Net::HTTP.new('awebsite',443)http.use_ssl=truehttp.verify_mode=OpenSSL::SSL::VERIFY_NONEhttp.start(){|http|req=Net::HTTP::Get.new("image.jpg")req.basic_authlogin,passwordrespon
我在扩展一个在gem中定义并且是ActiveRecord::Base的子类的类时遇到问题。我唯一想扩展这个类的是:有很多:promos然而,扩展倾向于排除原始类。我得到的错误:PGError:ERROR:relation"sites"doesnotexistLINE4:WHEREa.attrelid='"sites"'::regclass^:SELECTa.attname,format_type(a.atttypid,a.atttypmod),d.adsrc,a.attnotnullFROMpg_attributeaLEFTJOINpg_attrdefdONa.attrelid=d.a
就这么简单:我怎样才能得到Browser.text.include?,或者一般的Ruby,对指定的命令不区分大小写? 最佳答案 最简单的方法之一是将您正在阅读的文本小写或大写:Browser.text.downcase.include?然后,您需要确保以全部小写形式提供所需的文本。 关于ruby-我怎样才能得到Browser.text.include?不区分大小写?,我们在StackOverflow上找到一个类似的问题: https://stackoverfl