参数'snapshot'由于其类型而不能具有"null"的值,但隐式默认值为"null"



我得到这个错误。我试过把需要的词,但它显示新的错误。有人能帮帮我吗?我被困在这一点上了。如果我把这个写进去。PostDetails

输入图片描述

输入图片描述

我代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
//ignore: must_be_immutable
class PostDetails extends StatefulWidget {
DocumentSnapshot snapshot;
PostDetails(List<DocumentSnapshot<Object?>> snapshot, {required this.snapshot});
@override
_PostDetailsState createState() => _PostDetailsState();
}
class _PostDetailsState extends State<PostDetails> {
get title => null;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Post Details"),
backgroundColor: Colors.green,
),
body: new Card(
elevation: 10.0,
margin: EdgeInsets.all(10.0),
child: new ListView(
children: <Widget>[
new Container(
padding: EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
new CircleAvatar(
child: Text(widget.snapshot["title"][0]),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
new SizedBox(
width: 10.0,
),
new Text(
widget.snapshot[title],
style: TextStyle(fontSize: 22.0, color: Colors.green),
),
],
)),
new SizedBox(
height: 7.0,
),
new Container(
margin: EdgeInsets.all(1.0),
child: new Text(
widget.snapshot["content"],
style: TextStyle(fontSize: 18.0),
),
)
],
),
),
);
}
}

更新:我得到了这个错误输入图片描述

update:在另一个文件的快照中得到错误我的代码:

class _HomeState extends State<Home> {
late StreamSubscription<QuerySnapshot> subscription;
late List<DocumentSnapshot> snapshot;
CollectionReference collectionReference =
Firestore.instance.collection("Post");
Color gradientStart = Colors.deepPurple[700]!;
Color gradientEnd = Colors.purple[500]!;
passData(DocumentSnapshot snap) {
Navigator.of(this.context).push(new MaterialPageRoute(
builder: (context) => PostDetails(
snapshot,
)));
}

StatefulWidget的部件部分必须是不可变的。将快照属性标记为final

class PostDetails extends StatefulWidget {
final DocumentSnapshot snapshot;
PostDetails({required this.snapshot});
@override
_PostDetailsState createState() => _PostDetailsState();
}

你也有冲突的构造函数参数名称…不知道你在用未命名的位置参数做什么。参数名必须是唯一的,即使一个是位置的,一个是命名的。想想如果你有一个构造函数体…身体怎么知道哪个是哪个呢

相关内容

最新更新