我有关于Firestore的数据。我使用json和bloc模式进行序列化。 项目未运行。错误是
未处理的异常:NoSuchMethodError:方法'[]'被调用 空值。接收方:null尝试呼叫:
我的DB.dart文件(用于设置数据)
class DB {
final db = Firestore.instance;
getPhotography() async {
await db
.collection('photography')
.document('0yUc5QBGHNNq6WK9CyyF')
.setData(jsonDecode(jsonEncode(Photography(
[AllImages(["list"])],
"image_url",
"Shanika",
"inage_url",
"lovely couple",
"Bhanuka"
))));
}}
DB db = DB();
photography_bloc类
class PhotographyBloc extends BlocBase{
PhotographyBloc(){
init();
}
Photography photography;
final _firestoreController = StreamController<Photography>();
Stream<Photography> get outFirestore => _firestoreController.stream;
Sink<Photography> get inFirestore => _firestoreController.sink;
@override
void dispose() {
_firestoreController.close();
}
void init() async{
Photography data;
await db.getPhotography().then((v) => data = v);
inFirestore.add(data);
}
}
Widget(StreamBuilder)
child: StreamBuilder<Photography>(
stream: bloc.outFirestore,
initialData: null,
builder: (context, snapshot) {
print(snapshot.data); //output is null
Photography data = Photography.fromJson(jsonDecode(jsonEncode(snapshot.data))); //try to retrieving data, error is coming here
摄影模型类(我使用过build_runner)
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);
}