我正在用新版本从旧版本中学习颤振。所以,我多次遇到空值安全问题。
我在database.dart
文件中有这样的代码:
import 'package:cloud_firestore/cloud_firestore.dart';
class DatabaseService {
final String uid;
DatabaseService({required this.uid});
}
当我添加“必需”时它可以工作并且没有出现错误,但是我不能在文件中使用 DatabaseService()
参数:home.dart
class Home extends StatelessWidget {
Home({Key? key}) : super(key: key);
final AuthService _auth = AuthService();
@override
Widget build(BuildContext context) {
return StreamProvider<QuerySnapshot?>.value(
initialData: null,
value: DatabaseService().brews,
child: Scaffold(),
}
}
错误home.dart
是
The named parameter 'uid' is required, but there's no corresponding argument.
Try adding the required argument.
而且,如果我不添加requiredDatabaseService({this.uid})
那么错误将出现在database.dart
The parameter 'uid' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
那么我如何DatabaseService()
在其他文件中使用?