我需要从Firestore数据库中获取所有数据。 我遵循这种方式,https://stackoverflow.com/a/55865045/9139407
$('li.nav-item').click(function() {
id = $(this).data('id');
name = $(this).data('name');
rate = $(this).data('rate');
value = $(this).data('value');
$('#' + id + '_name').empty();
$('#' + id + '_rate').empty();
$('#' + id + '_rate').attr('data-item-rate', 0);
if(value != 0) {
value_sign = '$' + value;
$('#' + id + '_name').html('<strong>' + name + '</strong>');
$('#' + id + '_rate').html(value_sign);
$('#' + id + '_rate').attr('data-item-rate', value);
}
updateTotal();
});
// Update the total
function updateTotal() {
var total = 0;
$('p[data-item-rate]').each(function() {
total += isNaN(parseInt($(this).data('item-rate'))) ? 0 : parseInt($(this).data('item-rate'));
});
total = total + ' $';
$('#total_estimate').html(total);
}
错误消息是:
未处理的异常:类型'Future
'不是类型的子类型 'Future <摄影>'
摄影模型课
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-lg-8">
<div class="row">
<div class="col-lg-6">
<h2>Photos gallery</h2>
</div>
<div class="col-lg-6">
<ul class="nav nav-tabs-outline">
<li class="nav-item" data-id="gallery" data-name="Photos gallery" data-rate="No" data-value="0">
<a class="nav-link active" data-toggle="tab" href="#">
No
</a>
</li>
<li class="nav-item" data-id="gallery" data-name="Galerie photos" data-rate="Yes" data-value="300">
<a class="nav-link " data-toggle="tab" href="#">
Yes
<span class="badge badge-secondary">+ 300 $</span>
</a>
</li>
</ul>
</div>
</div>
<hr>
<div class="row">
<div class="col-lg-6">
<h2>Contact form</h2>
</div>
<div class="col-lg-6">
<ul class="nav nav-tabs-outline">
<li class="nav-item" data-id="contact-form" data-name="Contact form" data-rate="No" data-value="0">
<a class="nav-link active" data-toggle="tab" href="#">
No
</a>
</li>
<li class="nav-item" data-id="contact-form" data-name="Formulaire de contact" data-rate="Yes" data-value="250">
<a class="nav-link " data-toggle="tab" href="#">
Yes
<span class="badge badge-secondary">+ 250 $</span>
</a>
</li>
</ul>
</div>
</div>
<hr>
<div class="row">
<div class="col-lg-6">
<h2>Newsletter</h2>
</div>
<div class="col-lg-6">
<ul class="nav nav-tabs-outline">
<li class="nav-item" data-id="newsletter" data-name="Abonnement infolettre" data-rate="No" data-value="0">
<a class="nav-link active" data-toggle="tab" href="#">
No
</a>
</li>
<li class="nav-item" data-id="newsletter" data-name="Abonnement infolettre" data-rate="Yes" data-value="250">
<a class="nav-link " data-toggle="tab" href="#">
Yes
<span class="badge badge-secondary">+ 250 $</span>
</a>
</li>
</ul>
</div>
</div>
<hr>
</div>
<div class="col-lg-4">
<div class="cart-price">
<div class="flexbox">
<div>
<p id="gallery_name"></p>
<p id="gallery_rate" data-item-rate="0"></p>
<hr />
<p id="contact-form_name"></p>
<p id="contact-form_rate" data-item-rate="0"></p>
<hr />
<p id="newsletter_name"></p>
<p id="newsletter_rate" data-item-rate="0"></p>
</div>
</div>
<hr>
<div class="flexbox">
<div>
<p><strong>Total:</strong></p>
</div>
<div>
<p class="fw-600" id="total_estimate">0 $</p>
</div>
</div>
</div>
</div>
下一个问题是,如何将这些数据添加到集团接收器上?
Future<Photography> getPhotography() {
Future<Photography> data = await db
.collection('photography')
.document('0yUc5QBGHNNq6WK9CyyF')
.setData(jsonDecode(jsonEncode(Photography(
[AllImages(["list"])],
"image_url",
"Shanika",
"image_url",
"lovely couple",
"Bhanuka"
))));
return data;
}
像这样?但是错误是,参数类型'Future'不能分配给参数类型'Photography'
import 'package:json_annotation/json_annotation.dart';
part 'Model.g.dart';
@JsonSerializable()
class Photography{
List<AllImages> all_images;
String couplePhoto;
String female;
String image_url;
String info;
String male;
Photography();
factory Photography.fromJson(Map<String, dynamic> json) => _$PhotographyFromJson(json);
Map<String,dynamic> toJson() => _$PhotographyToJson(this);
}
@JsonSerializable()
class AllImages {
final List<String> imageUrl;
AllImages(this.imageUrl);
factory AllImages.fromJson(Map<String, dynamic> json) => _$AllImagesFromJson(json);
Map<String,dynamic> toJson() => _$AllImagesToJson(this);
}
答案 0 :(得分:1)
Document.setData()
返回Future<void>
-表示将来完成时不会返回任何数据。当然,对于二传手来说这是合理的。这就是为什么您不能将其变成任何东西的原因。目前尚不清楚您打算从哪里获得data
。您从一个空(?)Photography
开始,对其进行编码,解码并存储,然后期望返回某些内容。什么?原来的空物体? (您可以退还-但我认为那不是您想要的。)