我有 2 个收藏:电影、类型。在电影收藏中,存在类型作为列表参考。
在我的应用程序中,我想从收藏电影中的单个文档中获取所有流派列表值。
论点 1:某人电影的参考类型列表
参数 2:列表连接的格式分隔符
测试函数返回错误,在构建中返回默认值但引用不为空
我如何从该文档参考中获取值?
import 'dart:convert';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
String? joinGenresFromFilm(
List<DocumentReference>? filmGenresRef,
String separator,
) {
/// MODIFY CODE ONLY BELOW THIS LINE
// get from filmGenresRef document all ru_name fields and join to string
if (filmGenresRef == null || filmGenresRef.isEmpty) {
return null;
}
List<String> genres = [];
for (DocumentReference ref in filmGenresRef) {
ref.get().then((snapshot) {
if (snapshot.exists) {
Map<String, dynamic> data = snapshot.data()! as Map<String, dynamic>;
String ruName = data['ru_name'];
genres.add(ruName);
}
});
}
return genres.join(separator);
/// MODIFY CODE ONLY ABOVE THIS LINE
}
更新:
我在编辑器的测试功能中收到此错误:
Compilation error: Info: Compiling with sound null safety
lib/main.dart:23:9:
Error: The method 'get' isn't defined for the class 'DocumentReference'.
- 'DocumentReference' is from 'package:dartpad_sample/main.dart' ('lib/main.dart').
ref.get().then((snapshot) {
try it:
Future<String?> joinGenresFromFilm(
List<DocumentReference>? filmGenresRef,
String separator,
) async {
/// MODIFY CODE ONLY BELOW THIS LINE
// get from filmGenresRef document all ru_name fields and join to string
if (filmGenresRef == null || filmGenresRef.isEmpty) {
return null;
}
List<String> genres = [];
for (DocumentReference ref in filmGenresRef) {
DocumentSnapshot snapshot = await FirebaseFirestore.instance.doc(ref.path).get();
if (snapshot.exists) {
Map<String, dynamic> data = snapshot.data()! as Map<String, dynamic>;
String ruName = data['ru_name'];
genres.add(ruName);
}
}
return genres.join(separator);
/// MODIFY CODE ONLY ABOVE THIS LINE
}