The syntax seems to have changed with the update of flutter. I tried to find a solution on the official site, but failed.
import 'package:flutter/material.dart';
import 'package:my_app1/model/model_member.dart';
class MainScreen extends StatefulWidget {
final Member member;
DetailScreen({this.member});
_DetailScreenState createState() => _DetailScreenState();
}
Error: The parameter 'member' can't have a value of 'null' because of its type 'Member', but the implicit default value is 'null'.
and also
Widget _buildBody(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('member').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return _buildList(context, snapshot.data.docs);
},
);
}
Error: Property 'docs' cannot be accessed on 'QuerySnapshot<Object?>?' because it is potentially null.
CodePudding user response:
Try this:
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection("member")
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> chatSnapshot) {
if (chatSnapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Container(),
);
}
return ListView(
reverse: true,
controller: _controller,
physics: const BouncingScrollPhysics(),
children:
chatSnapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return Text(data["name"]);
}).toList(),
);
},
),
CodePudding user response:
The main change from flutter 1.0 to 2.0 is Null safety
. So it will make sure that the member
field is not null. so adding required in front of the member
should fix the problem
class MainScreen extends StatefulWidget {
final Member member;
DetailScreen({required this.member}); // <-- change this line
_DetailScreenState createState() => _DetailScreenState();
}