我有下面的代码,它从 firestore 读取 baby 名字和投票,并用 thump_up 和 thump_down 按钮表示 Cards 中的信息.
数据在 firebase 数据库更改时更新,但更新错误,例如在下面的屏幕截图中,首先我输入 Karam 婴儿数据,它正确更新,然后我输入'Dana' 婴儿数据,但它用 Karam 婴儿数据创建了另一张卡片(即之前创建的同一张卡片),但是当输入 Yara 婴儿数据时,正确的卡片有专为这里而生!我想是因为它在列表的末尾!
此外,在上次截屏中,我从数据库中删除了Dana baby 的记录,但删除的是最后创建的卡,是Yara 宝贝!!
同样,一旦我们点击 thumb_up 图标,投票就会在数据库中更新,但不会反射(reflect)在卡片中 :(
main.dart:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'BabyModel.dart';
import 'BabyCard.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
MyApp();
@override
State<StatefulWidget> createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
@override
Widget build(BuildContext ctxt) {
return StreamBuilder(
stream: Firestore.instance.collection('baby').snapshots(),
builder: (_, AsyncSnapshot<QuerySnapshot> snapshot) {
var documents = snapshot.data?.documents ?? [];
var baby =
documents.map((snapshot) => BabyData.from(snapshot)).toList();
return BabyPage(baby);
},
);
}
}
class BabyPage extends StatefulWidget {
final List<BabyData> allBaby;
BabyPage(this.allBaby);
@override
State<StatefulWidget> createState() {
return BabyPageState();
}
}
class BabyPageState extends State<BabyPage> {
@override
Widget build(BuildContext context) {
// var filteredBaby = widget.allFish.where((BabyData data) {
// data.name = 'Dana';
// }).toList();
return MaterialApp(
debugShowCheckedModeBanner: false,
home: SafeArea(
child: Scaffold(
body: Container(
child: ListView.builder(
itemCount: widget.allBaby.length,
padding: const EdgeInsets.only(top: 10.0),
itemBuilder: (context, index) {
return BabyCard(widget.allBaby[index]);
})
),
)));
}
}
BabyModel.dart:
import 'package:cloud_firestore/cloud_firestore.dart';
class BabyData {
final DocumentReference reference;
String name;
int vote;
BabyData.data(this.reference,
[this.name,
this.vote]) {
// Set these rather than using the default value because Firebase returns
// null if the value is not specified.
this.name ??= 'Frank';
this.vote ??= 7;
}
factory BabyData.from(DocumentSnapshot document) => BabyData.data(
document.reference,
document.data['name'],
document.data['vote']);
void save() {
reference.setData(toMap());
}
Map<String, dynamic> toMap() {
return {
'name': name,
'vote': vote,
};
}
}
BabyCard.dart:
import 'package:flutter/material.dart';
import 'BabyModel.dart';
class BabyCard extends StatefulWidget {
final BabyData baby;
BabyCard(this.baby);
@override
State<StatefulWidget> createState() {
return BabyCardState(baby);
}
}
class BabyCardState extends State<BabyCard> {
BabyData baby;
String renderUrl;
BabyCardState(this.baby);
Widget get babyCard {
return
new Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: const Icon(Icons.album),
title: Text('The ${baby.name} is having:'),
subtitle: Text('${baby.vote} Votes.'),
),
new ButtonTheme.bar( // make buttons use the appropriate styles for cards
child: new ButtonBar(
children: <Widget>[
new FlatButton(
child: const Icon(Icons.thumb_up),
onPressed: () => Firestore.instance.runTransaction((transaction) async {
DocumentSnapshot freshSnap =
await transaction.get(baby.reference);
await transaction.update(
freshSnap.reference, {'vote': freshSnap['vote'] + 1});
}),
),
new FlatButton(
child: const Icon(Icons.thumbs_up_down),
onPressed: () { /* ... */ },
),
new FlatButton(
child: const Icon(Icons.thumb_down),
onPressed: () { /* ... */ },
)]))]));
}
@override
Widget build(BuildContext context) {
return new Container(
child: babyCard,
);
}
}
下面显示错误的数据添加到卡中,Dana baby not there:
下面显示从卡中删除错误数据,Yara 婴儿卡已被删除,在删除 Dana 婴儿记录时!:
最佳答案
您可以使用常规小部件作为流构建器的单元,而不是使用有状态小部件作为您的单元格。这在某种程度上是该错误的解决方法,但实际上可能无法解决您的问题。
关于firebase - Firebase 更新时 Flutter 的错误行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51029912/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie